• 0

[C++] Read in values with spaces


Question

Hi. I want to read in 3 seperate values that the user enters but they are sperated by spaces. So A 1 4, A is one value, 1 is another, and 4 is the third value. How would I do this? Thanks in advance.

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Let's try something new. I'll help but I want to see what you have tried first. Don't take this the wrong way but failing and trying at a language is the best way to learn. So, while I know what you need I would like to see what you have so far and I'll make a few suggestions.

Link to comment
Share on other sites

  • 0

ok i figured it out.. I just ask the user for the info: A 1 4 and the user types it in

and I just have a

cin >> type;

cin >> vertex;

cin >> vertex2;

But I want it to quit asking for info when the user pushes 'q' .... But when I push 'q' it is still waiting for other values such as the vertex and vertex2... hmm.. not sure how to get around this.

Link to comment
Share on other sites

  • 0
haha.. this is the only thing i can think of:

cout << "Enter the edge " << endl;

cin >> type;

while (type != 'q')

{

cin >> vertex;

cin >> vertex2;

}

Well, that's fine if you repeatedly ask for a type (which you're not doing...what you've got there is an infinite loop since if they don't enter q the first time, it'll just keep asking for vertex and vertex2). If you're only asking once, check with an if statement.

Link to comment
Share on other sites

  • 0

but i am repeatedly asking for the three values and when I get them I do some code .. for example R 1 4... then i do some code... then i want to ask for 3 more values such as A 3 4.. but if the user enters just 1 value of Q..i want it to quit the program...

Link to comment
Share on other sites

  • 0
but i am repeatedly asking for the three values and when I get them I do some code .. for example R 1 4... then i do some code... then i want to ask for 3 more values such as A 3 4.. but if the user enters just 1 value of Q..i want it to quit the program...

cin>>type;

while (type != 'q') {

cin >> vertex;

cin >> vertex2;

dosomething();

cin >> type;

}

finishup();

Link to comment
Share on other sites

  • 0
thnaks a lot!!!!! i forgot to read it in again.. therefore i think i created an infinite loop...

Yep, like I said, unless they enter q the first time, it would go into an infinite loop.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.