Having trouble with a else... else if loop (For a class)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 17
Reputation: Avaviel is an unknown quantity at this point 
Solved Threads: 0
Avaviel Avaviel is offline Offline
Newbie Poster

Having trouble with a else... else if loop (For a class)

 
0
  #1
Oct 31st, 2008
My code, in 'full' (only 89 lines, lol) is here. My input file is here and (for kicks and giggles) my outuput file when I comment out the else... if statements and have simply fout is here. (Remove the .txt on the last two...)

I'm having a problem with my if else statement. When I comment out the else if stuff, it works.

(Choice is an int)

  1. if (choice == 1)
  2. {
  3. fout << fixed << showpoint << setprecision(2);
  4. fout << "The average female gpa is " << AverageFemaleGPA << endl << endl;
  5. fout << "The average male gpa is " << AverageMaleGPA << endl << endl;
  6. fout << "The overall gpa is " << AverageGPA << endl;
  7. }
  8. else if (choice == 2)
  9. {
  10. cout << fixed << showpoint << setprecision(2);
  11. cout << "The average female gpa is " << AverageFemaleGPA << endl << endl;
  12. cout << "The average male gpa is " << AverageMaleGPA << endl << endl;
  13. cout << "The overall gpa is " << AverageGPA << endl;
  14. }

Is is supposed to be something like <= and not ==? Choice does either equal 1 or 2, and if it is 3 it skips all of them to the end. But the above code skips over them, even though for it to get out of the while loop it HAD to work.

Does it have something with the preceding do while loop?
  1. do
  2. {
  3. cout << "What would you like to do?" << endl << endl;
  4. cout << "1. Output the gpa information to an output file." << endl << endl;
  5. cout << "2. Display the gpa information on the screen." << endl << endl;
  6. cout << "Exit the program." << endl << endl;
  7. cout << "Enter your selection:";
  8. cin >> choice;
  9. }
  10. while (choice!=1 && choice!=2 && choice!=3);
I'm sure I made a stupid mistake... indeed, for a while It did not work: I was giving choice the value of 2 or 1 each time! ( else if (choice = 2))

Thanks for the help! I have this due tomorrow, so no matter what I'll be getting it done. I hate getting all of the work (I know easy for some of you, but I'm a beginner) and then something simple stupid not letting it work. And I KNOW a switch would work, but the prof does not want one!

I name my files funny, by the way. The name of the project? Poppus_of_sorrowz. I believe I have more fun making the name than the code itself... although making kickbutt code, above and beyond what the professor wants, is fun. But I'm going for KISS (Keep it simple, stupid) for now, as I don't have time for very pretty code, with extra stuff (as much as I would like to do that! School can be a pain, so much to study for!
Last edited by Avaviel; Oct 31st, 2008 at 1:10 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: Avaviel is an unknown quantity at this point 
Solved Threads: 0
Avaviel Avaviel is offline Offline
Newbie Poster

Re: Having trouble with a else... else if loop (For a class)

 
0
  #2
Oct 31st, 2008
um. yeas.

So I DID NOTHING to the code, except let a friend look at it and uncomment the hated stupid section... and it worked. Stupid.

Thanks for looking!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Having trouble with a else... else if loop (For a class)

 
0
  #3
Oct 31st, 2008
Don't ever ever try to get cute with your code. Professors really seriously hate that.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 3
Reputation: UMAIR37 is an unknown quantity at this point 
Solved Threads: 0
UMAIR37 UMAIR37 is offline Offline
Newbie Poster

Re: Having trouble with a else... else if loop (For a class)

 
0
  #4
Oct 31st, 2008
I think you have not included iostream or have not written using namespace std;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC