944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4558
  • C++ RSS
Sep 14th, 2004
1

need help w/for loop

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib> //for rand numbers
  2. #include <ctime> //for srand numbers
  3. #include <iostream> //for all programs
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int j; // this will int these variables
  10. int k;
  11. int num1;
  12. int num2;
  13. int product;
  14. int answer;
  15. int correctAnswer; // this will int these variables
  16. cout << "Welcome to the Times Table Tutor!";
  17. cout << endl;
  18. cout << "This program will help you learn the multiplication tables.";
  19. cout << endl;
  20. cout << "You will have to answer 10 question correctly before the program will end.";
  21. cout << endl;
  22.  
  23. for (num1 = 0; num2 <=10; num1++)
  24. {
  25.  
  26. srand((unsigned)time(0));
  27.  
  28. j = 0 + rand() % 12;
  29.  
  30. k = 0 + rand() % 12;
  31.  
  32. cout << num1 * num2;
  33.  
  34. product = num1 * num2;
  35.  
  36. cin >> answer;
  37.  
  38. cout << " Way to go, keep up the good work!";
  39.  
  40. while ( !(correctAnswer == answer)) // ask for the right answer
  41. {
  42. cout << "WRONG, Lets try it again";
  43. cin >> answer;
  44. }
  45. cout << "Yea, your correct, I knew you could do it!"; // this should keep the loop going to complete the program
  46.  
  47. }
  48.  
  49. char userInput;
  50. cin >> userInput;
  51.  
  52. return 0;
  53. }
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
Last edited by Nick Evan; Nov 23rd, 2011 at 2:22 pm.
Similar Threads
Reputation Points: 13
Solved Threads: 0
Newbie Poster
big buc's fan is offline Offline
11 posts
since Sep 2004
Sep 14th, 2004
1

Re: need help w/for loop

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.
C++ Syntax (Toggle Plain Text)
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3.  
  4. int product;
  5. int answer;
  6. int correct = 0;
  7. int wrong = 0;
  8.  
  9.  
  10. for ( int i = 1; i <= 3; i++ )
  11.  
  12. for( int j = 0; j <= 12; j++ ){
  13. cout << "Chloie what is " << i << " X " << j << " = ";
  14. cin >> answer;
  15. product = i * j;
  16.  
  17. if( answer == product ) {
  18. cout <<"Good Job Chloie!!!!!" << endl << endl;
  19. correct++;
  20. }
  21. else {
  22. cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
  23. wrong++;
  24. }
  25. }
  26.  
  27. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  28. cout <<"Go get your dad chloie....." << endl;
  29. cin.get();
  30.  
  31. return 0;
  32. }

Hope it helps
Reputation Points: 14
Solved Threads: 0
Newbie Poster
big146 is offline Offline
18 posts
since Jul 2004
Sep 14th, 2004
1

Re: need help w/for loop

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 ;
}
Reputation Points: 12
Solved Threads: 0
Newbie Poster
vijay choudhari is offline Offline
2 posts
since Sep 2004
Sep 14th, 2004
0

Re: need help w/for loop

Quote 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.
C++ Syntax (Toggle Plain Text)
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3.  
  4. int product;
  5. int answer;
  6. int correct = 0;
  7. int wrong = 0;
  8.  
  9.  
  10. for ( int i = 1; i <= 3; i++ )
  11.  
  12. for( int j = 0; j <= 12; j++ ){
  13. cout << "Chloie what is " << i << " X " << j << " = ";
  14. cin >> answer;
  15. product = i * j;
  16.  
  17. if( answer == product ) {
  18. cout <<"Good Job Chloie!!!!!" << endl << endl;
  19. correct++;
  20. }
  21. else {
  22. cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
  23. wrong++;
  24. }
  25. }
  26.  
  27. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  28. cout <<"Go get your dad chloie....." << endl;
  29. cin.get();
  30.  
  31. return 0;
  32. }

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
Reputation Points: 13
Solved Threads: 0
Newbie Poster
big buc's fan is offline Offline
11 posts
since Sep 2004
Sep 14th, 2004
0

Re: need help w/for loop

Quote 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
Reputation Points: 13
Solved Threads: 0
Newbie Poster
big buc's fan is offline Offline
11 posts
since Sep 2004
Sep 14th, 2004
0

Re: need help w/for loop

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.
C++ Syntax (Toggle Plain Text)
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3. int product;
  4. int sum;
  5. int answer;
  6. int correct = 0;
  7. int wrong = 0;
  8. int menu;
  9.  
  10.  
  11. do {
  12. cout << setw( 55 ) << "Math Tutor Progam For Lauren" << endl;
  13. cout << setw( 47 ) << "Author:Ben" << endl << endl;
  14. cout << setw( 49 ) << "*******Menu*******" << endl;
  15. cout << setw( 50 ) << "[1] Multiplacation " << endl;
  16. cout << setw( 44 ) << "[2] Addition " << endl;
  17. cout << setw( 47 ) << "[3] Subtraction " << endl;
  18. cout << setw( 40 ) << "[4] Exit " << endl << endl;
  19. cout <<"Please enter your choice: ";
  20. cin >> menu;
  21.  
  22. switch ( menu )
  23. {
  24. case 1:
  25. for ( int i = 1; i <= 12; i++ )
  26.  
  27. for( int j = 0; j <= 12; j++ ){
  28.  
  29. cout << "Lauren what is " << i << " X " << j << " = ";
  30. cin >> answer;
  31. product = i * j;
  32.  
  33. if( answer == product ) {
  34. cout <<"Good Job Lauren!!!!!" << endl << endl;
  35. correct++;
  36. }
  37. else {
  38. cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
  39. wrong++;
  40. }
  41. }
  42.  
  43. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  44. cout <<"Go get your dad Lauren....." << endl;
  45. cin.get();
  46. break;
  47.  
  48. case 2:
  49. for ( int i = 1; i <= 12; i++ )
  50.  
  51. for( int j = 0; j <= 12; j++ ){
  52.  
  53. cout << "Lauren what is " << i << " + " << j << " = ";
  54. cin >> answer;
  55. sum = i + j;
  56.  
  57. if( answer == sum ) {
  58. cout <<"Good Job Lauren!!!!!" << endl << endl;
  59. correct++;
  60. }
  61. else {
  62. cout << "Sorry " << i <<" + " << j << " = " << sum << endl << endl;
  63. wrong++;
  64. }
  65. }
  66.  
  67. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  68. cout <<"Go get your dad Lauren....." << endl;
  69. cin.get();;
  70. break;
  71.  
  72. case 3:
  73. for ( int i = 12; i <= 22; i++ )
  74.  
  75. for( int j = 1; j <= 12; j++ ){
  76.  
  77. cout << "Lauren what is " << i << " - " << j << " = ";
  78. cin >> answer;
  79. sum = i - j;
  80.  
  81. if( answer == sum ) {
  82. cout <<"Good Job Lauren!!!!!" << endl << endl;
  83. correct++;
  84. }
  85. else {
  86. cout << "Sorry " << i <<" - " << j << " = " << sum << endl << endl;
  87. wrong++;
  88. }
  89. }
  90.  
  91. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  92. cout <<"Go get your dad Lauren....." << endl;
  93. cin.get();
  94. break;
  95.  
  96. default:
  97. break;
  98.  
  99. }
  100.  
  101. }while ( menu != 4 );
  102. return 0;
  103. }
Reputation Points: 14
Solved Threads: 0
Newbie Poster
big146 is offline Offline
18 posts
since Jul 2004
Sep 14th, 2004
0

Re: need help w/for loop

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.
Reputation Points: 14
Solved Threads: 0
Newbie Poster
big146 is offline Offline
18 posts
since Jul 2004
Sep 15th, 2004
0

Re: need help w/for loop

Quote 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.
C++ Syntax (Toggle Plain Text)
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3. int product;
  4. int sum;
  5. int answer;
  6. int correct = 0;
  7. int wrong = 0;
  8. int menu;
  9.  
  10.  
  11. do {
  12. cout << setw( 55 ) << "Math Tutor Progam For Lauren" << endl;
  13. cout << setw( 47 ) << "Author:Ben" << endl << endl;
  14. cout << setw( 49 ) << "*******Menu*******" << endl;
  15. cout << setw( 50 ) << "[1] Multiplacation " << endl;
  16. cout << setw( 44 ) << "[2] Addition " << endl;
  17. cout << setw( 47 ) << "[3] Subtraction " << endl;
  18. cout << setw( 40 ) << "[4] Exit " << endl << endl;
  19. cout <<"Please enter your choice: ";
  20. cin >> menu;
  21.  
  22. switch ( menu )
  23. {
  24. case 1:
  25. for ( int i = 1; i <= 12; i++ )
  26.  
  27. for( int j = 0; j <= 12; j++ ){
  28.  
  29. cout << "Lauren what is " << i << " X " << j << " = ";
  30. cin >> answer;
  31. product = i * j;
  32.  
  33. if( answer == product ) {
  34. cout <<"Good Job Lauren!!!!!" << endl << endl;
  35. correct++;
  36. }
  37. else {
  38. cout << "Sorry " << i <<" X " << j << " = " << product << endl << endl;
  39. wrong++;
  40. }
  41. }
  42.  
  43. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  44. cout <<"Go get your dad Lauren....." << endl;
  45. cin.get();
  46. break;
  47.  
  48. case 2:
  49. for ( int i = 1; i <= 12; i++ )
  50.  
  51. for( int j = 0; j <= 12; j++ ){
  52.  
  53. cout << "Lauren what is " << i << " + " << j << " = ";
  54. cin >> answer;
  55. sum = i + j;
  56.  
  57. if( answer == sum ) {
  58. cout <<"Good Job Lauren!!!!!" << endl << endl;
  59. correct++;
  60. }
  61. else {
  62. cout << "Sorry " << i <<" + " << j << " = " << sum << endl << endl;
  63. wrong++;
  64. }
  65. }
  66.  
  67. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  68. cout <<"Go get your dad Lauren....." << endl;
  69. cin.get();;
  70. break;
  71.  
  72. case 3:
  73. for ( int i = 12; i <= 22; i++ )
  74.  
  75. for( int j = 1; j <= 12; j++ ){
  76.  
  77. cout << "Lauren what is " << i << " - " << j << " = ";
  78. cin >> answer;
  79. sum = i - j;
  80.  
  81. if( answer == sum ) {
  82. cout <<"Good Job Lauren!!!!!" << endl << endl;
  83. correct++;
  84. }
  85. else {
  86. cout << "Sorry " << i <<" - " << j << " = " << sum << endl << endl;
  87. wrong++;
  88. }
  89. }
  90.  
  91. cout << "You got " << correct <<" right and " << wrong << " wrong" << endl<< endl;
  92. cout <<"Go get your dad Lauren....." << endl;
  93. cin.get();
  94. break;
  95.  
  96. default:
  97. break;
  98.  
  99. }
  100.  
  101. }while ( menu != 4 );
  102. return 0;
  103. }
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
Reputation Points: 13
Solved Threads: 0
Newbie Poster
big buc's fan is offline Offline
11 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Beginner programmer
Next Thread in C++ Forum Timeline: names in ascending order problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC