| | |
Quick question on a loop.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
Hi guys, quick question for you.
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.
C++ Syntax (Toggle Plain Text)
for (i = 0; i < NUM_EMPS; i++) { cout << "First and Last Name: "; cin.getline( names[i], 31 ); for (index = 0; index < NUM_MONTHS; index++) { cout << "Hours worked in "; cout << months[index]; cout << ": "; cin >> hours[i][index]; } }
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.
•
•
Join Date: Feb 2008
Posts: 67
Reputation:
Solved Threads: 1
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.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
int main() { const int NUM_EMPS = 3; const int NUM_MONTHS = 6; char months[6][9] = {"January", "February", "March", "April", "May", "June"}; char names[NUM_EMPS][31]; float hours[NUM_EMPS][NUM_MONTHS]; int index, i; cout << "header"; cout << "Enter the following information:\n\n"; // INPUT for (i = 0; i < NUM_EMPS; i++) { cout << "First and Last Name: "; cin.getline( names[i], 31 ); for (index = 0; index < NUM_MONTHS; index++) { cout << "Hours worked in "; cout << months[index]; cout << ": "; cin.getline( hours[i], index ); } }
I appreciate any advice.
Last edited by yoyoman3000; Feb 11th, 2008 at 2:17 am.
•
•
Join Date: Feb 2008
Posts: 67
Reputation:
Solved Threads: 1
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:
Here is what i got:
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { const int NUM_EMPS = 3; const int NUM_MONTHS = 6; char months[6][9] = {"January", "February", "March", "April", "May", "June"}; char names[NUM_EMPS][31]; float hours[NUM_EMPS][NUM_MONTHS]; int index, i; string input; cout << "header"; cout << "Enter the following information:\n\n"; // INPUT for (i = 0; i < NUM_EMPS; i++) { cout << "First and Last Name: "; cin.getline( names[i], 31 ); for (index = 0; index < NUM_MONTHS; index++) { cout << "Hours worked in "; cout << months[index]; cout << ": "; getline (cin, input); stringstream(input) >> hours[i][index]; } } }
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { const int NUM_EMPS = 3; const int NUM_MONTHS = 6; char months[6][9] = {"January", "February", "March", "April", "May", "June"}; char names[NUM_EMPS][31]; float hours[NUM_EMPS][NUM_MONTHS]; int index, i; string input; cout << "header"; cout << "Enter the following information:\n\n"; // INPUT for (i = 0; i < NUM_EMPS; i++) { cout << "First and Last Name: "; cin.getline( names[i], 31 ); for (index = 0; index < NUM_MONTHS; index++) { cout << "Hours worked in "; cout << months[index]; cout << ": "; getline (cin, input); stringstream(input) >> hours[i][index]; } } }
![]() |
Similar Threads
- quick question (C++)
- Quick Question (VB.NET)
- quick question (C)
- reading input ... quick C++ question ... (C++)
Other Threads in the C++ Forum
- Previous Thread: need help asap
- Next Thread: Problem in compiling simple program!!!
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





