Homework Help

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?





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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<string>

using namespace 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;
}
Last edited on
Could you edit your code and use code tags, so it's easier to read - http://www.cplusplus.com/articles/jEywvCM9/

Also, why would you be using VB 2008, it's 2016 you know?
First time posting. Thanks for the tip. I am using VB 2008 because my professor insists it is the best one.
Perhaps because he is into dinosaurs, since that's what they used back in their days.
XD, do you think you could help me out? Do you see anything obviously wrong with the Float portion of this? I feel like I did this right.
I don't think casting works that way. Try using setPrecision - http://www.cplusplus.com/reference/iomanip/setprecision/
Last edited on
Thanks, I havent seen this before, so it makes me uneasy that he would make us do this. I shall give it a shot though.
I ran a test and I was wrong, casting does indeed work that way.

1
2
3
4
int x = 5;
int y = 6;

cout << (float)x / (float)y << endl;


This game me 0.833333. Run it and tell me what it gives you.
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.
Last edited on
I am just in shock that this works on IDE but not in VB. I am getting frustrated.
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.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<string>

using namespace 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;
}
Last edited on
@bdanielz doesnt cin read until there is a space? I thought it could read even a string.
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>

using namespace 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.


Last edited on
Topic archived. No new replies allowed.