I can't seem to input two numbers on the same line

Hey. Worlds worst programmer here again. I have a question as far as input is concerned. Now, we're supposed to be using the cin to input two numbers on the same line. Piece of cake right? No problem right? Well, with namespace std it is. Unfortunately, I can't do that with this course. I'm supposed to use the string buffer to input the multiple values. My question is, how do I do that? The code I have below compiles, but it just hangs and gives no output. What am I doing wrong? Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  // Lab 3, Change
// Programmer: Jesse Burns
// Editor(s) used: JNotePad
// Compiler(s) used: VC++ 2010 Express
// The necessary C++ file library

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <string>
using std::string;

#include <cstdlib>

int main()
{

      // Jesse Burns, Lab 3
  cout << "Lab 3, Change\n";
  cout << "Programmer: Jesse Burns\n";
  cout << "Editor(s) used: JNotePad\n";
  cout << "Compiler(s) used: VC++ 2010 Express\n";
  cout << "File: " << __FILE__ << endl;
  cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl << endl;

  double x, y;
  string buf1, buf2;

  cout << "Enter two numbers on the same line and they will be outputted."<<endl;
  cin >> x >> y;
  cin >> buf1; x = atof(buf1.c_str());
  cin >> buf2; y = atof(buf2.c_str());

  cout << "The output is "<< x << " and " << y << endl;

}
@jackbruns28

You're inputting the two numbers using the x & y variables. Then you try inputting two more numbers with the buf1 and buf2, then overwrite the first two inputs in x and y. Just use the buf1 and buf2 variables first, and then use the atof() conversions.

Like so..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Lab 3, Change
// Programmer: Jesse Burns
// Editor(s) used: JNotePad
// Compiler(s) used: VC++ 2010 Express
// The necessary C++ file library

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <string>
using std::string;

#include <cstdlib>

int main()
{

      // Jesse Burns, Lab 3
  cout << "Lab 3, Change\n";
  cout << "Programmer: Jesse Burns\n";
  cout << "Editor(s) used: JNotePad\n";
  cout << "Compiler(s) used: VC++ 2010 Express\n";
  cout << "File: " << __FILE__ << endl;
  cout << "Compiled: " << __DATE__ << " at " << __TIME__ << endl << endl;

  double x, y;
  string buf1, buf2;

  cout << "Enter two numbers on the same line and they will be outputted."<<endl;
  cin >> buf1 >> buf2;
  x = atof(buf1.c_str());
  y = atof(buf2.c_str());

  cout << "The output is "<< x << " and " << y << endl;

}
Last edited on
Whew. Thanks man. You're the only one that pointed out what I was doing wrong. Even my C++ instructor couldn't help me.
Glad I could help, jackbruns28..
Topic archived. No new replies allowed.