Substitute cin with "getline" when storing values for operation.

So I am still learning about C++. I'm trying to substitute cin with getline. So instead of storing values using cin, I use getline. And it doesn't work, can you explain? This is my first post of question and I'm still new, this is my first account. Thank you.

#include <iostream>
#include <string>
#include<conio.h>
using namespace std;
int main()
{
string answer1, answer2, sum;
cout << "Enter first number: " << endl;
getline (cin,answer1);
cout << "Enter second number: " <<endl;
getline (cin, answer2);
sum=answer1+answer2;
cout << "The sum is: " << sum <<endl;
getch();
}



Output is:

Enter first number:
1
Enter second number:
2
The sum is: 12.


The sum is wrong, it didn't even use ADDITION.

But I understand that getline is awesome because when you store something, it includes the spaces unlike cin.

My observation is that: "Did it identify "answer1" and "answer 2" variables as a string BUT not as an integer?" If I used integer, I can't use the "getline" anymore. So... what should I do.


Last edited on
It's because you are adding 2 strings and not two numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;
int main()
{
	string answer1, answer2, sum;
	cout << "Enter first number: " << endl;
	getline(cin, answer1);
	cout << "Enter second number: " << endl;
	getline(cin, answer2);

// atoi -> converts string to int
// c_str() -> converts a nice string to a c-style array t
	double number1 = atoi(answer1.c_str());
	double number2 = atoi(answer2.c_str());

	cout << "The sum is: " << number1 + number2 << endl;


	return 0;
}
Last edited on
I'm trying to substitute cin with getline.

Why?
Because I think getline can do MORE, like for example both "string" and numeric values to perform arithmetic operations on them. Is there a way to identify them as numeric value without changing string into "integer" type?

For example:

cout << "Enter first number: " << endl;

getline (cin, answer1) <<<< in this part, how can you convert it to integer. THEN...

cout << "Enter second number: " << endl;
getline (cin, answer2) <<<<< in this part again, make it integer before performing it in arithmetic operation..

sum=answer1+answer2

cout << "The sum is: " << sum << endl << "." endl;

So in the end it will identify as integer and display the SUM.
Last edited on
in this part, how can you convert it to integer
You cannot. YOu will have to do it later, after you get the string.

By using stoi for example:
1
2
3
4
std::getline(std::cin, temp);
int answer1 = std::stoi(temp);
std::getline(std::cin, temp);
int answer2 = std::stoi(temp);
The fundamental problem is the user. The user can and will write "fubar" even though your program clearly prompts to enter an unsigned int.

There are many ways to perform the conversion, but the real challenge is how you test for and handle the erroneous input. The atoi() does not indicate failure. The stoi() throws an exception. The operator>> formatted input sets the failbit of the stream.

Note that the getline() can fail too for other reasons.


Ok, phase 1 is done; you detect error in some way.
Then you have phase 2 to decide: what to do next?
Do you abort the program, or keep asking the same data again, until the user complies?

1. istreamobj >> answer
2. if error
  clear istreamobj error flags
  ignore current istreamobj content
  repeat from 1
@keskiverto

This is what I was looking for, I saw it in the tutorial. // stringstreams

#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;

int main ()
{
string mystr;
float price=0;
int quantity=0;

cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity << endl;
getch();
}



It was a string, then it turned into an integer.
I just want to warn you, stringstream constructor is painfully slow. It is better to have a single stingsteam object and reuse it.
Topic archived. No new replies allowed.