Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-30-2004, 09:25 PM   #1 (permalink)
nightcrawler
Registered User
 
nightcrawler's Avatar
 
Join Date: Sep 2004
Posts: 3
nightcrawler is on a distinguished road
Question Counter

I am doing a flashcard program using C# in which I need to display 2 single digit numbers and wait for the user to enter the answer. Then I need to display the score , which is numberCorrect/numberAttempted * 100. I have my program running fine right now, but the only problem is to do the counter for the numberCorrect and numberAttempted. I used do/while loop, and for whatever reason it only went through the loop once!!! HELP!!!
Code:
	do			
			{
				Random flashcard = new Random();  // create new random object

				int a = flashcard.Next(0,10);	// first number
				int b = flashcard.Next(0,10);  //second number
			
				int answer;
				int correct = 0; 
				int attempt = 0;
				

				Console.Write(a + " + " + b + " = " );
				input = Console.ReadLine();
				
				//If the child enter q or Q, exit program
				if ((input == "q") || (input == "Q"))
					break;
				
				//if answer correct, increment and diplay correct message. 
				//otherwise display incorrect message and the correct answer
				answer = Int32.Parse(input);
				if (answer == (a+b))
				{	
					correct++;
					attempt++;
					Console.WriteLine("Corret! Good Job!");
						
				}			
				else
				{	
					attempt++;
					Console.WriteLine("Wrong answer. The correct answer is " + (a+b));
						
				}
			
				Console.WriteLine("attempt = " + attempt);
				Console.WriteLine("correct = " + correct);
							
				//display score
				Console.WriteLine("Score = " + Calculate(correct, attempt));
								
			}
			while ((input != "q") || (input != "Q"));

Last edited by sde; 10-04-2004 at 05:08 AM.
nightcrawler is offline   Reply With Quote
Old 10-04-2004, 05:12 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
you are setting 'correct' and 'attempt' to 0 each time the values go through the loop. put those lines of code above the 'do'. also, we have tags which make code easier to read here. [code ] [/code ] ( without the spaces.

look at the top before the do loop. i think this will solvey our problem.
Code:
int answer;
int correct = 0; 
int attempt = 0;

do			
{
	Random flashcard = new Random();  // create new random object

	int a = flashcard.Next(0,10);	// first number
	int b = flashcard.Next(0,10);  //second number

	Console.Write(a + " + " + b + " = " );
	input = Console.ReadLine();
	
	//If the child enter q or Q, exit program
	if ((input == "q") || (input == "Q"))
		break;
	
	//if answer correct, increment and diplay correct message. 
	//otherwise display incorrect message and the correct answer
	answer = Int32.Parse(input);
	if (answer == (a+b))
	{	
		correct++;
		attempt++;
		Console.WriteLine("Corret! Good Job!");
			
	}			
	else
	{	
		attempt++;
		Console.WriteLine("Wrong answer. The correct answer is " + (a+b));
			
	}

	Console.WriteLine("attempt = " + attempt);
	Console.WriteLine("correct = " + correct);
				
	//display score
	Console.WriteLine("Score = " + Calculate(correct, attempt));
					
}
while ((input != "q") || (input != "Q"));
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
text prediction C toblerone Standard C, C++ 5 08-03-2004 03:49 AM
Counter Strike Ilya020 Lounge 12 04-26-2003 10:22 AM
Counter Strike server Ilya020 Lounge 27 03-10-2003 05:10 PM


All times are GMT -8. The time now is 07:13 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting