Problem with cin to variables

JamesL1518
JamesL1518 used Ask the Experts™
on
I've been working on this code for a little while now and think I finally got what I needed with this line of code.
for (int i = 0; i < 3; i++){
cin>> submission_times[i]>>names[i]>>priorities[i]>>execution_times[i];
}

When I put a cout line in there to see the input, it was all working fine.  Unfortunately, as the loop is done, the program exits too instead of going on to the next part of the code.  I put just a simple cout line to after the code to see if it would reach it and it never does.  I've tried other ways of inputting the data, but this is the only way I could get the code to do what I wanted (which is store the data to different variables over multiple lines).  If someone could help me out with whatever is wrong with this or has a good alternative to it, it would be great.  Thanks.
#include <sstream>
#include <string>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "queue.h"
#include <vector>
using namespace std;
 
//char **Explode(char *str);
 
void displayVector (const vector<string>& v, int i)
{
for (int k = 0; k < i; ++k)
{
cout << "The complete input is:" << k << ": " << v[k] << endl;
}
}
 
int main() {
   Queue queue;
   int max_jobs=20;
   int time=0; //use counter from header file
   istringstream strLine;
   string line, word;
   int priorities[max_jobs];
   int execution_times[max_jobs];
   int submission_times[max_jobs];
   vector<string> names(max_jobs);
  int currentLine=0;  //Also keeps track of number of jobs
  int counter=0;
  
for (int i = 0; i < 3; i++){
cin>> submission_times[i]>>names[i]>>priorities[i]>>execution_times[i];
}
cout<<priorities[0];
string Item; //To prevent program from closing right after it runs
cin>> Item;
return 0;
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
cin >> Item; doesn't really prevent closing the program. Either run the program in terminal, or use this:

cin.clear();
while (cin.ignore());
cin.clear();
char c; cin >> c;

Open in new window

evilrixSenior Software Engineer (Avast)

Commented:
Have you tried putting a break point on the closing brace of main() and running it in the debugger?

Also, if you are coding on Windows you can also add the following to the end of your program

system("pause");

You'll need to include <cstdlib> for this to work.
I think that this may be the problem:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6

The last "cin>>...>>>>execution_times[i];" according to that website should be the culprit.  Below, I offer two solutions that should work.

Also, on a side note, in your "displayVector" function, I believe you meant to do "k++" in the loop, not "++k".

//solution #1
for (int i = 0; i < 3; i++){
cin>> submission_times[i]>>names[i]>>priorities[i]>>execution_times[i];
}
  //line added
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 
cout<<priorities[0];
string Item; //To prevent program from closing right after it runs
cin>> Item;
return 0;
 
//solution #2
for (int i = 0; i < 3; i++){
cin>> submission_times[i]>>names[i]>>priorities[i]>>execution_times[i];
}
cout<<priorities[0];
string Item; //To prevent program from closing right after it runs
cin>> Item;
  //Line added
cin>> Item;
return 0;

Open in new window

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Top Expert 2009

Commented:
>> or has a good alternative to it

Don't use cin << for input. Use getline to read the input one line at a time (into a string buffer), and then get whatever values you need out of the string buffer :

        http://cplusplus.com/reference/string/getline/

Doing this will make your code a lot more robust, and you won't need to worry about the state of your stream (like clear any trailing characters, check the stream state after every input operation, etc.).


>> but this is the only way I could get the code to do what I wanted (which is store the data to different variables over multiple lines).

Do you want to store the data, or process it ? Because you can process it as soon as you read it ... That way, you don't have to put it in memory, which is especially useful for bigger amounts of input data. Plus, you would at least see the progress sooner, instead of having to read all the input data first before seeing the results.

Author

Commented:
WhiteMage, I'm going to try your code in just a second and see if that was the problem.  From that link, it looks like that could be what was happening.

>> Do you want to store the data, or process it ? Because you can process it as soon as you read it ... That way, you don't have to put it in memory, which is especially useful for bigger amounts of input data. Plus, you would at least see the progress sooner, instead of having to read all the input data first before seeing the results.

I do want to store the input into something easily accessible.  All the input will be processed at the end anyways.  I originally was using getline, but it wasn't able to split the string up to the 4 different arrays I needed for each line.  Or at least not as easily as cin.

Author

Commented:
Perfect, thanks.  I can get the input just fine now.
Top Expert 2009

Commented:
>> All the input will be processed at the end anyways.

My point is : why not process it while you're reading the input ? Is there a specific need to process it at the end ?


>> I originally was using getline, but it wasn't able to split the string up to the 4 different arrays I needed for each line.

getline reads an entire line of input into a string, which you'll then have to split up as required.
If you like the way you split it up using cin, then you could consider using a stringstream to split up the string (as shown below).

Note that the use of getline is there for robustness, and precisely to avoid the kind of problems you are currently having. By using getline, you keep the input stream clean, and that is very important.
std::string line;
while (getline(std::cin, line)) {
  std::stringstream ss(line);
  ss >> submission_times[i] >> names[i] >> priorities[i] >> execution_times[i];
}

Open in new window

Top Expert 2009

Commented:
Please do consider not to use the accepted approach. You'll save yourself a whole lot of trouble and headaches if you use only getline for input. Trust me :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial