User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,579 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,747 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 3260 | Replies: 7
Reply
Join Date: Sep 2004
Location: fayetteville nc
Posts: 11
Reputation: big buc's fan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
big buc's fan big buc's fan is offline Offline
Newbie Poster

need help w/for loop

  #1  
Sep 13th, 2004
First i wanted to say thanks to the ones who help me with my first thread :-)
i was able to get that program running with some more coding.
now w.the problem i have.
Iam trying to write a multiplication program that will help my kids memorize his time table.

As a new coder i keep getting these errors about " local variable 'num2' used without having been initialized" i thought i did can any one point me in the right direction. i have attach the whole program, if i'm not initializing at the right place then where should it be. i have the book called "STARTING OUT WITH C++ 4th ed." i read thee chapther about the loops and while /do while/for loops and i guess i mess something up.. any help would be great.

#include <cstdlib> //for rand numbers
#include <ctime> //for srand numbers
#include <iostream> //for all programs

using namespace std;

int main()
{
int j; // this will int these variables
int k;
int num1;
int num2;
int product;
int answer;
int correctAnswer; // this will int these variables
cout << "Welcome to the Times Table Tutor!";
cout << endl;
cout << "This program will help you learn the multiplication tables.";
cout << endl;
cout << "You will have to answer 10 question correctly before the program will end.";
cout << endl;

for (num1 = 0; num2 <=10; num1++)
{

srand((unsigned)time(0));

j = 0 + rand() % 12;

k = 0 + rand() % 12;

cout << num1 * num2;

product = num1 * num2;

cin >> answer;

cout << " Way to go, keep up the good work!";

while ( !(correctAnswer == answer)) // ask for the right answer
{
cout << "WRONG, Lets try it again";
cin >> answer;
}
cout << "Yea, your correct, I knew you could do it!"; // this should keep the loop going to complete the program

}

char userInput;
cin >> userInput;

return 0;
}

error 1=warning C4700: local variable 'num2' used without having been initialized
error2=warning C4700: local variable 'correctAnswer' used without having been initialized


thanks for all that help
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: need help w/for loop

  #2  
Sep 14th, 2004
Hey there!
Funny, I wrote the same thing awhile back when i first started I'll post the code so you can take a look at it. Just change the for loop to the desired number of itterations that you want or just use it as a template.This also was for my daughter.I another one for my older daugheter(11) that does multiplacation, subtraction, and addition, if your interested let me know and ill post the code for ya..anyway here it is.
int _tmain(int argc, _TCHAR* argv[])
{
	
	int product;
	int answer;
	int correct = 0;
	int wrong = 0;


	for ( int i = 1; i <= 3; i++ )

		for( int j = 0; j <= 12; j++ ){
			cout << "Chloie what is " << i << " X " << j << " = ";
			cin >> answer;
			product = i * j;

			if( answer == product ) {
				cout <<"Good Job Chloie!!!!!" << endl << endl;
				correct++;
			}
			else {
				cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
				wrong++;
			}
		}

		cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
		cout <<"Go get your dad chloie....." << endl;
		cin.get();

		return 0;
}

Hope it helps
big146
Reply With Quote  
Join Date: Sep 2004
Posts: 2
Reputation: vijay choudhari is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vijay choudhari vijay choudhari is offline Offline
Newbie Poster

Re: need help w/for loop

  #3  
Sep 14th, 2004
Hi,

I have modified your program to remove unused variables and unnecessary code. Hope this works out for you.

Regards,
vijay.

-----------------------------------------------------

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std ;

int main()
{
int j; // this will int these variables
int num1;
int num2;
int product;
int answer;
cout << "Welcome to the Times Table Tutor!";
cout << endl;
cout << "This program will help you learn the multiplication tables.";
cout << endl;
cout << "You will have to answer 10 question correctly before the program will end.";
cout << endl;

for (j = 1; j <= 10; j++) {
srand((unsigned)time(0));

num1 = 0 + rand() % 12;

num2 = 0 + rand() % 12;

cout << num1 << " * " << num2 << " = " ;

product = num1 * num2;

cin >> answer;

cout << " Way to go, keep up the good work!";

while ( !(product == answer)) // ask for the right answer
{
cout << "WRONG, Lets try it again ";
cin >> answer;
}
cout << "Yea, your correct, I knew you could do it!" << endl ;

}
return 0 ;
}
Reply With Quote  
Join Date: Sep 2004
Location: fayetteville nc
Posts: 11
Reputation: big buc's fan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
big buc's fan big buc's fan is offline Offline
Newbie Poster

Re: need help w/for loop

  #4  
Sep 14th, 2004
Originally Posted by big146
Hey there!
Funny, I wrote the same thing awhile back when i first started I'll post the code so you can take a look at it. Just change the for loop to the desired number of itterations that you want or just use it as a template.This also was for my daughter.I another one for my older daugheter(11) that does multiplacation, subtraction, and addition, if your interested let me know and ill post the code for ya..anyway here it is.
int _tmain(int argc, _TCHAR* argv[])
{
	
	int product;
	int answer;
	int correct = 0;
	int wrong = 0;


	for ( int i = 1; i <= 3; i++ )

		for( int j = 0; j <= 12; j++ ){
			cout << "Chloie what is " << i << " X " << j << " = ";
			cin >> answer;
			product = i * j;

			if( answer == product ) {
				cout <<"Good Job Chloie!!!!!" << endl << endl;
				correct++;
			}
			else {
				cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
				wrong++;
			}
		}

		cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
		cout <<"Go get your dad chloie....." << endl;
		cin.get();

		return 0;
}

Hope it helps


Thanks allot for your help.
about your offer w/ the add/subtraction/div that would great.
i was reading in my C++ book about how you can do a loop of which question(what do you want to do (*/-+) and then go do the problem would that be a nested loop.

any way i would like see your program, it would get me started as to how to build one simple math program that helps them but that doesn't lose their attention..
again thanks for the help and if you cant find that's ok thanks again
Reply With Quote  
Join Date: Sep 2004
Location: fayetteville nc
Posts: 11
Reputation: big buc's fan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
big buc's fan big buc's fan is offline Offline
Newbie Poster

Re: need help w/for loop

  #5  
Sep 14th, 2004
Originally Posted by vijay choudhari
Hi,

I have modified your program to remove unused variables and unnecessary code. Hope this works out for you.

Regards,
vijay.

-----------------------------------------------------

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std ;

int main()
{
int j; // this will int these variables
int num1;
int num2;
int product;
int answer;
cout << "Welcome to the Times Table Tutor!";
cout << endl;
cout << "This program will help you learn the multiplication tables.";
cout << endl;
cout << "You will have to answer 10 question correctly before the program will end.";
cout << endl;

for (j = 1; j <= 10; j++) {
srand((unsigned)time(0));

num1 = 0 + rand() % 12;

num2 = 0 + rand() % 12;

cout << num1 << " * " << num2 << " = " ;

product = num1 * num2;

cin >> answer;

cout << " Way to go, keep up the good work!";

while ( !(product == answer)) // ask for the right answer
{
cout << "WRONG, Lets try it again ";
cin >> answer;
}
cout << "Yea, your correct, I knew you could do it!" << endl ;

}
return 0 ;
}


yep
it did the trick..
i kept try to do the right thing at the wrong area..
this is fun but can fustrating.. but as any good programmer knows its takes allot of sleepless nights and ughh to finnally see his/her errors(i made a funny) mans it been a long night.
again thanks for the help.
any other simple programs that will help me with a simple math program that can incompass all the small 4(-*/+) math operators in one, like a question to ask which one you want to do selection, then do the amout of question, then if they want to try it again, then make the selection, ask the amout of question, once done then ask then term with no response.. that would be a nested upon a nested upon a nested right..
again thanks for the help and sorry about the lenght of the reply..
i'm trying to get better but at snail pace but want to be world class sprinter,, iand i'm off, ugg more errors, im out of here
Reply With Quote  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: need help w/for loop

  #6  
Sep 14th, 2004
At your request....here's the code.I havent changed it since i wrote it. I like to look back and see how I used to do things....hope this helps ya.
int _tmain(int argc, _TCHAR* argv[])
{
	int product;
	int sum;
	int answer;
	int correct = 0;
	int wrong = 0;
	int menu;


	do {	
		cout << setw( 55 ) << "Math Tutor Progam For Lauren" << endl;
		cout << setw( 47 ) << "Author:Ben" << endl << endl;
		cout << setw( 49 ) << "*******Menu*******" << endl;
		cout << setw( 50 ) << "[1] Multiplacation " << endl;
		cout << setw( 44 ) << "[2] Addition " << endl;
		cout << setw( 47 ) << "[3] Subtraction " << endl;
		cout << setw( 40 ) << "[4] Exit " << endl << endl;
		cout <<"Please enter your choice: ";
		cin >> menu;

		switch ( menu )
		{
		case 1:
			for ( int i = 1; i <= 12; i++ )

				for( int j = 0; j <= 12; j++ ){

					cout << "Lauren what is " << i << " X " << j << " = ";
					cin >> answer;
					product = i * j;

					if( answer == product ) {
						cout <<"Good Job Lauren!!!!!" << endl << endl;
						correct++;
					}
					else {
						cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
						wrong++;
					}
				}

				cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
				cout <<"Go get your dad Lauren....." << endl;
				cin.get();
				break;

		case 2:
			for ( int i = 1; i <= 12; i++ )

				for( int j = 0; j <= 12; j++ ){

					cout << "Lauren what is " << i << " + " << j << " = ";
					cin >> answer;
					sum = i + j;

					if( answer == sum ) {
						cout <<"Good Job Lauren!!!!!" << endl << endl;
						correct++;
					}
					else {
						cout << "Sorry " << i <<" + " << j << " = " << sum << endl << endl;
						wrong++;
					}
				}

				cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
				cout <<"Go get your dad Lauren....." << endl;
				cin.get();;
				break;

		case 3:
			for ( int i = 12; i <= 22; i++ )

				for( int j = 1; j <= 12; j++ ){

					cout << "Lauren what is " << i << " - " << j << " = ";
					cin >> answer;
					sum = i - j;

					if( answer == sum ) {
						cout <<"Good Job Lauren!!!!!" << endl << endl;
						correct++;
					}
					else {
						cout << "Sorry " << i <<" - " << j << " = " << sum << endl << endl;
						wrong++;
					}
				}

				cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
				cout <<"Go get your dad Lauren....." << endl;
				cin.get();
				break;

		default:
			break;

		}

	}while ( menu != 4 );
	return 0;
}
big146
Reply With Quote  
Join Date: Jul 2004
Posts: 18
Reputation: big146 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
big146's Avatar
big146 big146 is offline Offline
Newbie Poster

Re: need help w/for loop

  #7  
Sep 14th, 2004
Just another word of advice....dont focus so much on how to write your own functions at this point.Instead focus on the syntax and learning what it is you can and cant do.

Maybe pick up a book on the STL to read when you dont feel like sitting at the comp typing away.The STL is full of algorithms and functions that can do 95% of what you need to do..without having to write your own. You just need to learn what it is that the STL can do for you and how to write it.
big146
Reply With Quote  
Join Date: Sep 2004
Location: fayetteville nc
Posts: 11
Reputation: big buc's fan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
big buc's fan big buc's fan is offline Offline
Newbie Poster

Re: need help w/for loop

  #8  
Sep 15th, 2004
Originally Posted by big146
At your request....here's the code.I havent changed it since i wrote it. I like to look back and see how I used to do things....hope this helps ya.
int _tmain(int argc, _TCHAR* argv[])
{
	int product;
	int sum;
	int answer;
	int correct = 0;
	int wrong = 0;
	int menu;


	do {	
		cout << setw( 55 ) << "Math Tutor Progam For Lauren" << endl;
		cout << setw( 47 ) << "Author:Ben" << endl << endl;
		cout << setw( 49 ) << "*******Menu*******" << endl;
		cout << setw( 50 ) << "[1] Multiplacation " << endl;
		cout << setw( 44 ) << "[2] Addition " << endl;
		cout << setw( 47 ) << "[3] Subtraction " << endl;
		cout << setw( 40 ) << "[4] Exit " << endl << endl;
		cout <<"Please enter your choice: ";
		cin >> menu;

		switch ( menu )
		{
		case 1:
			for ( int i = 1; i <= 12; i++ )

				for( int j = 0; j <= 12; j++ ){

					cout << "Lauren what is " << i << " X " << j << " = ";
					cin >> answer;
					product = i * j;

					if( answer == product ) {
						cout <<"Good Job Lauren!!!!!" << endl << endl;
						correct++;
					}
					else {
						cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
						wrong++;
					}
				}

				cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
				cout <<"Go get your dad Lauren....." << endl;
				cin.get();
				break;

		case 2:
			for ( int i = 1; i <= 12; i++ )

				for( int j = 0; j <= 12; j++ ){

					cout << "Lauren what is " << i << " + " << j << " = ";
					cin >> answer;
					sum = i + j;

					if( answer == sum ) {
						cout <<"Good Job Lauren!!!!!" << endl << endl;
						correct++;
					}
					else {
						cout << "Sorry " << i <<" + " << j << " = " << sum << endl << endl;
						wrong++;
					}
				}

				cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
				cout <<"Go get your dad Lauren....." << endl;
				cin.get();;
				break;

		case 3:
			for ( int i = 12; i <= 22; i++ )

				for( int j = 1; j <= 12; j++ ){

					cout << "Lauren what is " << i << " - " << j << " = ";
					cin >> answer;
					sum = i - j;

					if( answer == sum ) {
						cout <<"Good Job Lauren!!!!!" << endl << endl;
						correct++;
					}
					else {
						cout << "Sorry " << i <<" - " << j << " = " << sum << endl << endl;
						wrong++;
					}
				}

				cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
				cout <<"Go get your dad Lauren....." << endl;
				cin.get();
				break;

		default:
			break;

		}

	}while ( menu != 4 );
	return 0;
}
yea i know what you mean when it coming know what each thing does in programming, thanks for the code, i should be able to plug my info into it and get it running, i do have a C++ book 4th edition breif version thought but it helps me allot.
any way about how long before i start to write code and don't get so fustrated with it. any way, have too fix the errors, have to fic the errrors, ughhh, have to fix errors... thanks again
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 10:43 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC