chosing which variable to use in writing programs

I'm supposed to write a program that performs division of two numbers read from the user & prints out an exact result...well I was able to write a program that does this but my result keeps saying 0 & I can't quite figure out how to get the two numbers to divide. Here's my program:

#include <iostream>

using namespace std;

int main()
{
int thisisanumber;
int whole_number;
int total;
total = thisisanumber / whole_number; // thisisanumber divided by whole_number
cout << "Please enter a number: \n";
cin >> thisisanumber;
cout << "Please enter another number:\n";
cin >> whole_number;
cout << "The total is:\n"; << total;

My program works but the result is not exact & I'm at a loss as to what exact "formula" I should use to divide two numbers and print out the correct exact result.
You are performing calculation with variables which were not initialized

1
2
3
4
int thisisanumber;
 int whole_number;
 int total;
 total = thisisanumber / whole_number; // thisisanumber divided by whole_number 


What are values of thisisanumber and whole_number?
When you do the division you have not yet given any values to thisisanumber and whole_number.
so I put int in front of total = thisisanumber....?
that gave me a "redeclaration of total" error message i already declared that variable & the program runs I just don't know how to get them to divide correctly....
At this line:
total = thisisanumber / whole_number; // thisisanumber divided by whole_number
What values do you think thisisanumber and whole_number has?
i give up just tell me I've been working on this all night & from what I've learned so far int is the only value option for total...I did that already in line 3

ohhhh....okay so I'm still clueless....lol I mean I get it but I don't get it...my brain is overloaded but I'm determined to get it so can u gimme a hint?

I read about float but I didn't think it applied to this...and hey I didn't do THAT bad lol..I'm learning all of this on my own so cut me sum slack huh? And thank you for your help! I really appreciate it....


okay....I rewrote it JUST as u told me & its still WRONG!!!! Now its coming back as 1.something something something.....so now what? Wait I was wrong....didn't catch where u had put:
total = thisisanumber / whole_number...etc....I knew I basically had the concept but didn't know what to do exactly thx again!
Last edited on
int is the type, not the value.

I know we shouldn't just , but this is painful :p

Megz, the things you need to work on are:

1) The order in which things happen - in your code, you were calculating the answer, and THEN you were getting the values from the user. Can you see why that won't work? If I want you to divide two numbers and tell me the answer, you need to know what those two numbers are BEFORE you can calculate the answer. I hope that's clear.

2) Understanding that the computer does not behave just like a human. In C++, when you divide one integer (you know what an integer is, yes?) by another, you will get back another integer. This mean, for example, that 5 divided by 2 equals 2, it does NOT equal 2.5; if you're going to programme, you have to know what numbers actually are inside the machine and how the basic mathematical operations apply to them.

I know these things sound really basic, but to code effectively you've got to realise that the machine doesn't "think" like a human. You'll see that I changed the types from int to float. It would be a good idea for you to look up int and float and understand the difference between them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
float thisisanumber;
float whole_number;
float total;
cout << "Please enter a number: \n";
cin >> thisisanumber;
cout << "Please enter another number:\n";
cin >> whole_number;
total = thisisanumber / whole_number; // thisisanumber divided by 
cout << "The total is:\n" << total;
}
Last edited on
Topic archived. No new replies allowed.