Quick question on a loop.

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

Join Date: Feb 2008
Posts: 5
Reputation: yoyoman3000 is an unknown quantity at this point 
Solved Threads: 0
yoyoman3000 yoyoman3000 is offline Offline
Newbie Poster

Quick question on a loop.

 
0
  #1
Feb 11th, 2008
Hi guys, quick question for you.

  1. for (i = 0; i < NUM_EMPS; i++)
  2. {
  3. cout << "First and Last Name: ";
  4. cin.getline( names[i], 31 );
  5.  
  6. for (index = 0; index < NUM_MONTHS; index++)
  7. {
  8. cout << "Hours worked in ";
  9. cout << months[index];
  10. cout << ": ";
  11. cin >> hours[i][index];
  12. }
  13. }

The first loop is supposed to prompt the user to enter a first and a last name. Then the second loop displays the name of a month, at which point the user inputs how may hours were worked that month. This is supposed to work for three different people. It works fine the first time through but when it comes time to enter the second name, it is skipped.

Sample Output:

First and Last Name: joe schmo
January: 1
Feb: 4
Mar: 6
Apr: 9
May: 12
June: 5

First and Last Name: January: <-------------
Feb:
Mar:
etc....


That arrow signifies where my issue is. Why is it skipping the input for name there? It doesn't pause at all?

Thanks for your time. I appreciate any help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Quick question on a loop.

 
0
  #2
Feb 11th, 2008
I had the same issues with one of my codes. I am no c++ expert so I cannot explain definite reasons but when I used getline() following the use of cin >> variable the getline was skipped, I found that the problem was fixed if I used all getline() functions in place of regular cin >> variable functions. I cannot explain why it works but I would guess it has something to do with the way the input is buffered. May sound like nonsense but thats what I would guess with the knowledge I have atm. Anyway, try replacing cin>>variable with getline() if not, I don't know what to tell you. Hope this helps. I used getline(cin, [string to store to]) then used sstream([string stored to]) >> variable in my codes to fix it. Hope that helps.
Last edited by c++noobie; Feb 11th, 2008 at 2:03 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: yoyoman3000 is an unknown quantity at this point 
Solved Threads: 0
yoyoman3000 yoyoman3000 is offline Offline
Newbie Poster

Re: Quick question on a loop.

 
0
  #3
Feb 11th, 2008
Thanks for the reply! I tried your suggestion and I'm getting a compiler error.

cannot convert parameter 1 from 'float [6]' to 'char *'

Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Quick question on a loop.

 
0
  #4
Feb 11th, 2008
Do you mind showing me the rest of the code? I may could help, I may not be able to, but I'm willing to try.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: yoyoman3000 is an unknown quantity at this point 
Solved Threads: 0
yoyoman3000 yoyoman3000 is offline Offline
Newbie Poster

Re: Quick question on a loop.

 
0
  #5
Feb 11th, 2008
  1. int main()
  2. {
  3. const int NUM_EMPS = 3;
  4. const int NUM_MONTHS = 6;
  5.  
  6. char months[6][9] = {"January", "February", "March", "April", "May", "June"};
  7. char names[NUM_EMPS][31];
  8. float hours[NUM_EMPS][NUM_MONTHS];
  9. int index, i;
  10.  
  11. cout << "header";
  12. cout << "Enter the following information:\n\n";
  13.  
  14. // INPUT
  15. for (i = 0; i < NUM_EMPS; i++)
  16. {
  17. cout << "First and Last Name: ";
  18. cin.getline( names[i], 31 );
  19.  
  20. for (index = 0; index < NUM_MONTHS; index++)
  21. {
  22. cout << "Hours worked in ";
  23. cout << months[index];
  24. cout << ": ";
  25. cin.getline( hours[i], index );
  26. }
  27. }


I appreciate any advice.
Last edited by yoyoman3000; Feb 11th, 2008 at 2:17 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Quick question on a loop.

 
0
  #6
Feb 11th, 2008
After working with it for a while, I think I have found the answer to your problem. If you use string stream to buffer the input for the hours worked then store that to the provided variable I believe it should work, I think this accomplishes your purposes, if not let me know.

Here is what i got:
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const int NUM_EMPS = 3;
  9. const int NUM_MONTHS = 6;
  10.  
  11. char months[6][9] = {"January", "February", "March", "April", "May", "June"};
  12. char names[NUM_EMPS][31];
  13. float hours[NUM_EMPS][NUM_MONTHS];
  14. int index, i;
  15. string input;
  16.  
  17. cout << "header";
  18. cout << "Enter the following information:\n\n";
  19.  
  20. // INPUT
  21. for (i = 0; i < NUM_EMPS; i++)
  22. {
  23. cout << "First and Last Name: ";
  24. cin.getline( names[i], 31 );
  25.  
  26. for (index = 0; index < NUM_MONTHS; index++)
  27. {
  28. cout << "Hours worked in ";
  29. cout << months[index];
  30. cout << ": ";
  31. getline (cin, input);
  32. stringstream(input) >> hours[i][index];
  33. }
  34. }
  35. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: yoyoman3000 is an unknown quantity at this point 
Solved Threads: 0
yoyoman3000 yoyoman3000 is offline Offline
Newbie Poster

Re: Quick question on a loop.

 
0
  #7
Feb 11th, 2008
Originally Posted by c++noobie View Post
After working with it for a while, I think I have found the answer to your problem. If you use string stream to buffer the input for the hours worked then store that to the provided variable I believe it should work, I think this accomplishes your purposes, if not let me know.

Here is what i got:
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const int NUM_EMPS = 3;
  9. const int NUM_MONTHS = 6;
  10.  
  11. char months[6][9] = {"January", "February", "March", "April", "May", "June"};
  12. char names[NUM_EMPS][31];
  13. float hours[NUM_EMPS][NUM_MONTHS];
  14. int index, i;
  15. string input;
  16.  
  17. cout << "header";
  18. cout << "Enter the following information:\n\n";
  19.  
  20. // INPUT
  21. for (i = 0; i < NUM_EMPS; i++)
  22. {
  23. cout << "First and Last Name: ";
  24. cin.getline( names[i], 31 );
  25.  
  26. for (index = 0; index < NUM_MONTHS; index++)
  27. {
  28. cout << "Hours worked in ";
  29. cout << months[index];
  30. cout << ": ";
  31. getline (cin, input);
  32. stringstream(input) >> hours[i][index];
  33. }
  34. }
  35. }
Thank you so much! I really do appreciate it. You fixed it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 67
Reputation: c++noobie is an unknown quantity at this point 
Solved Threads: 1
c++noobie c++noobie is offline Offline
Junior Poster in Training

Re: Quick question on a loop.

 
0
  #8
Feb 11th, 2008
Only glad I could help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: yoyoman3000 is an unknown quantity at this point 
Solved Threads: 0
yoyoman3000 yoyoman3000 is offline Offline
Newbie Poster

Re: Quick question on a loop.

 
0
  #9
Feb 11th, 2008
Originally Posted by c++noobie View Post
Only glad I could help.
Thanks again.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC