943,522 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1087
  • C++ RSS
Feb 11th, 2008
0

Quick question on a loop.

Expand Post »
Hi guys, quick question for you.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yoyoman3000 is offline Offline
5 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yoyoman3000 is offline Offline
5 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yoyoman3000 is offline Offline
5 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

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:
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

Click to Expand / Collapse  Quote originally posted by c++noobie ...
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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yoyoman3000 is offline Offline
5 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

Only glad I could help.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
c++noobie is offline Offline
71 posts
since Feb 2008
Feb 11th, 2008
0

Re: Quick question on a loop.

Click to Expand / Collapse  Quote originally posted by c++noobie ...
Only glad I could help.
Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
yoyoman3000 is offline Offline
5 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: need help asap
Next Thread in C++ Forum Timeline: Problem in compiling simple program!!!





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


Follow us on Twitter


© 2011 DaniWeb® LLC