Please Ignore the getline portion as I am still working on that. This question is for the float portion of the code. I need to display two different lines, one with the regular sum and the other with the float sum. When i build and debug in VB 2008 it does not display the 2nd line as a float, as in there is no decimal point showing up after the answer. Lets just focus on the addition one for now. Would anyone mind showing me what I am doing incorrectly?
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
int a;
int b;
char c;
int sum=0;
int diff=0;
int prod=1;
int div=1;
string name="";
cout << "Enter a number \n";
cin >> a;
cout << "Enter another number \n";
cin >> b;
cout <<"Enter an Arithmetic Operator \n";
cin >> c;
if (c=='+')
{
sum=a+b;
cout << "The sum of these numbers is " << sum << endl;
cout << "The sum of these numbers is " << float(sum) << endl;
if (sum > 1&& sum < 100)
{
cout << "The sum is between 1 and 100 " << endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
if (c=='-')
{
diff=a-b;
cout << "The difference of these numbers is " << diff << endl;
cout << "The difference of these numbers is " << float(diff) << endl;
if (diff > 1&& diff < 100)
{
cout << "The difference is between 1 and 100 " << endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
if (c=='*')
{
prod=a*b;
cout << "The product of these numbers is " << prod << endl;
cout << "The product of these numbers is " << float(prod) << endl;
if (prod > 1&& prod < 100)
{
cout << "The product is between 1 and 100 " << endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
if (c=='/')
{
div=a/b;
cout << "The dividend of these numbers is " << div << endl;
cout << "The dividend of these numbers is " << float(div) << endl;
if (div > 1&& div < 100)
{
cout << "The dividend is between 1 and 100 " <<endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
return 0;
}
When you enter in a float say "5.5"
cin reads until there are no more ints in your input. "5" then when it hits the "." it attempts to read that into your next var but fails, and does not read anything else and then goes to the next line not using cin.
So i even tried this just to be sure, and it still is not displaying the 2nd number as a float. Can someone please tell me that I am missing something? This is going against everything I've learned so far.
#include<iostream>
#include<string>
usingnamespace std;
int main()
{
int a;
int b;
char c;
int sum=0;
int diff=0;
int prod=1;
int div=1;
string name="";
cout << "Enter a number \n";
cin >> a;
cout << "Enter another number \n";
cin >> b;
cout <<"Enter an Arithmetic Operator \n";
cin >> c;
if (c=='+')
{
sum=a+b;
cout << "The sum of these numbers is " << sum << endl;
cout << "The sum of these numbers is " << sum*1.0 << endl;
if (sum > 1&& sum < 100)
{
cout << "The sum is between 1 and 100 " << endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
if (c=='-')
{
diff=a-b;
cout << "The difference of these numbers is " << diff << endl;
cout << "The difference of these numbers is " << diff*1.0 << endl;
if (diff > 1&& diff < 100)
{
cout << "The difference is between 1 and 100 " << endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
if (c=='*')
{
prod=a*b;
cout << "The product of these numbers is " << prod << endl;
cout << "The product of these numbers is " << prod*1.0 << endl;
if (prod > 1&& prod < 100)
{
cout << "The product is between 1 and 100 " << endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
if (c=='/')
{
div=a/b;
cout << "The dividend of these numbers is " << div << endl;
cout << "The dividend of these numbers is " << div*1.0 << endl;
if (div > 1&& div < 100)
{
cout << "The dividend is between 1 and 100 " <<endl;
cout << "Enter your first and last name with a space in between \n";
getline(cin, name);
}
}
return 0;
}
Try this code. then change the variables to double or float and try again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<iostream>
usingnamespace std;
int main() {
int a, b;
cout << "Enter 5.5" << endl;
cin >> a >> b;
cout << a << " " << b;
return 0;
}
Based on how the above code behaves I would suggest taking input as double then converting that to int if needed.
@bdanielz doesnt cin read until there is a space? I thought it could read even a string.
My understanding of cin (someone correct me if I am off)
Yes, cin reads until there is a space and while it receives what it was promised to get.
Say
if in the case cin receives an unexpected input, say a "." when reading an int.
Then cin goes into an error state and bails, until the error state is reset.