Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Quick question Posted by stahura07 on 11 Oct 2008 at 7:55 AM
I want this progam to loop as long as the user enters a positive number. If any other number is entered or q is entered to quit I want it to exit. For some reason it q is entered is just prints out forever. Any suggestions?
#include <iostream>
using namespace std;

int main( )
{
	
	int tablemax, tablenum, i, userInput;
	
	cout << "Enter the largest number you would like your multiplication table to test: ";
	cin >> tablemax;
	
	do 
	{
		cout << "To practice your multiplication, \n";
		cout << "enter the positive number you want to practice, or enter Q to quit.\n";
	
		cout << "You will practice this number up to " << tablemax << endl;
	
		cout << "Enter a number: ";
		cin >> tablenum; 

		int *table = new int [tablemax + 1];
 
 		for (i = 0; i <= tablemax; i++)
 		table[i] = tablenum * i;
 
		for (i = 0; i <= tablemax; i++)
		{
			cout << tablenum << " x " << i << " = ";
			cin >> userInput;
	
		if (userInput == table[i])
			cout << "Very good, that is the correct answer\n";
		else 
			cout << "That is incorrect, the correct answer is " << table[i] << endl;
		}		 
	
		cout << endl;
		cout << "You have completed your multiplication tables for: " << tablenum << endl;
		cout << endl; 
	}
	
	while (tablenum != 'q' || tablenum != 'Q');
		
		cout << "Goodbye!";
	
	return 0;
}

Report
Re: Quick question Posted by Lenizla on 11 Oct 2008 at 8:38 AM
: I want this progam to loop as long as the user enters a positive
: number. If any other number is entered or q is entered to quit I
: want it to exit. For some reason it q is entered is just prints out
: forever. Any suggestions?
:
: 
: #include <iostream>
: using namespace std;
: 
: int main( )
: {
: 	
: 	int tablemax, tablenum, i, userInput;
: 	
: 	cout << "Enter the largest number you would like your multiplication table to test: ";
: 	cin >> tablemax;
: 	
: 	do 
: 	{
: 		cout << "To practice your multiplication, \n";
: 		cout << "enter the positive number you want to practice, or enter Q to quit.\n";
: 	
: 		cout << "You will practice this number up to " << tablemax << endl;
: 	
: 		cout << "Enter a number: ";
: 		cin >> tablenum; 
: 
: 		int *table = new int [tablemax + 1];
:  
:  		for (i = 0; i <= tablemax; i++)
:  		table[i] = tablenum * i;
:  
: 		for (i = 0; i <= tablemax; i++)
: 		{
: 			cout << tablenum << " x " << i << " = ";
: 			cin >> userInput;
: 	
: 		if (userInput == table[i])
: 			cout << "Very good, that is the correct answer\n";
: 		else 
: 			cout << "That is incorrect, the correct answer is " << table[i] << endl;
: 		}		 
: 	
: 		cout << endl;
: 		cout << "You have completed your multiplication tables for: " << tablenum << endl;
: 		cout << endl; 
: 	}
: 	
: 	while (tablenum != 'q' || tablenum != 'Q');
: 		
: 		cout << "Goodbye!";
: 	
: 	return 0;
: }
: 
:


Variable "tablenum" is of integer type and you're assigning it character value.

Report
Re: Quick question Posted by Malcolm_McLean on 11 Oct 2008 at 9:33 AM
: while (tablenum != 'q' || tablenum != 'Q');
:
: cout << "Goodbye!";
:
Computers are, well, computer-like in their logic. While tablenum doesn't equal q or tablenum doesn't equal Q means that you do all the time, since if tablenum equals q it can't equal Q.

Report
Re: Quick question Posted by Ed Hall on 11 Oct 2008 at 11:31 AM
The two main trouble spots were noted by the others, but I added in a possible rewrite for your review:

#include <iostream>
using namespace std;

int main( )
{
	
	int tablemax, tablenum, i, userInput;
	char entry[5];
	
	cout << "Enter the largest number you would like your multiplication table to test: ";
	cin >> tablemax;
	
	do 
	{
		cout << "To practice your multiplication, \n";
		cout << "enter the positive number you want to practice, or enter Q to quit.\n";
	
		cout << "You will practice this number up to " << tablemax << endl;
	
		cout << "Enter a number: ";
		cin >> entry;

     if (entry[0] != 'q' && entry[0] != 'Q'){
		tablenum = atoi(entry);

		int *table = new int [tablemax + 1];
 
 		for (i = 0; i <= tablemax; i++)
 		table[i] = tablenum * i;
 
		for (i = 0; i <= tablemax; i++)
		{
			cout << tablenum << " x " << i << " = ";
			cin >> userInput;
	
		if (userInput == table[i])
			cout << "Correct\n";
		else 
			cout << "Incorrect.  Correct answer is " << table[i] << endl;
		}		 
	
		cout << endl;
		cout << "You have completed your multiplication tables for: " << tablenum << endl;
		cout << endl; 
     }	
	}
	
	while (entry[0] != 'q' && entry[0] != 'Q');
		
		cout << "Goodbye!\n\n\n";
	system("pause");
	return 0;
}

Take Care,
Ed



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.