[C++] Arithmatic Expressions problem.

Joined
Feb 5, 2008
Messages
601
Reaction score
0
It says:

For your project, use the method below to generate another way of looking at one's IQ:

Take someone's weight (in pounds).

Multiply it by shoe size.

Then divide the result of that by their height in feet (rounded to nearest foot).

Last, multiply by their age to come up with their IQ.

and heres my code which doesnt work -.-

#include <iostream>
#include <string>
using namespace std;
int main()
{
int lb;
char shoe;
string tall;
string age;

cout <<"Enter your weight (pounds): " << endl;
cin >> lb;
cout <<"Enter your shoe size: " << endl;
cin >> shoe;
cout <<"Enter your height: " << endl;
cin >> tall;
cout <<"Enter your age: " << endl;
cin >> age;

cout <<"Your IQ is: " << endl;


int a = age;
int b = smart;
int x = lb;
int y = shoe;
int z = tall;


x *= y; //Times varibles
cout << x << endl;

(x *= y)/=z; //subtract variables
cout << x << endl;

(x *= y /=z)*=a; //multiply variables
cout << x << endl;
system("PAUSE");
return 0;
}

can some one help out?
 
You convert the user input into a string for 2 inputs. A string is NOT a number! Well, it is, but not as you'd expect. It ranges from 0 to 255 per character (where number are somewhere around 30 I believe). And a string is a pointer to an array of those characters...however C++ strings are somewhat different, that's why learning C first is better, I think.

The point is, strings can't do arithmetic's, so what the code should look like, is this:
PHP:
#include <iostream>
#include <string>

int main()
{
unsigned int lb;
unsigned int shoe;
unsigned int tall;
unsigned int age;

std::cout << "Enter your weight (pounds): " << std::endl;
std::cin >> lb;
std::cout << "Enter your shoe size: " << std::endl;
std::cin >> shoe;
std::cout << "Enter your height: " << std::endl;
std::cin >> tall;
std::cout << "Enter your age: " << std::endl;
std::cin >> age;

std::cout << "Your IQ is: " << ((lb * shoe) / tall) * age) << std::endl;
system("PAUSE");
return 0;
}

I don't know what you did with the multiplications and divide's...but it was rather messy and redundant ^^. Also, you tried to rename the variables...why? It means you didn't choose a correct variable name in the beginning...but you did. You changed the variable names into something unreadable and unclear: the alphabet...please use descriptive names for variables, this saves you decade's trying to figure out what the problem is :P

Also, take a look at my formula:
FYI: unsigned means it can only be positive (the number).
((lb * shoe) / tall) * age)

What we do: multiply lb by shoe. Now it actually 'makes a new variable' which is the result of lb * shoe. With this 'new variable' we divide by tall, etc...this saves us creating new variables (and thus allocating on the RAM, which takes time) and just do arithmetics on the CPU, because the 'new variables' are kept on the CPU.

Tell me if you don't understand a part ;)
 
lmfao, yes the "usigned" part and std::endl, std::cout

Wow. Possibly the worst online course I have ever taken.

Write a program that asks for someone's weight.
**
Next, calculate how much they would weigh on the moon and the planets in our solar system and print out the results to one decimal place.
**
Use constants for keeping the different
weight factors.

Here are the factors:
Moon .166
Mercury .378
Venus .907
Mars .377
Jupiter 2.364
Saturn .916
Uranus .889
Neptune 1.125
Pluto .067

These people just EXPECT you to know how to do this, the labs never told me how to calculate it into decimals what the ****... Hell if i know how to write code for that.
 
Last edited:
It's not too difficult ;)

Use double's and write a function to round up to 1 decimal (something like, multiply by 10, round to whole numbers, divide by 10).

You can put the factors in an array, or use separate constants. Something like:

const double factors = {0.166, 0.378, 0.907, ...};
round_to_1_decimal_point(lb * factors[2]) = your weight on venus, rounded to 1 decimal point (the function you'll have to write yourself).
 
It's not too difficult ;)

Use double's and write a function to round up to 1 decimal (something like, multiply by 10, round to whole numbers, divide by 10).

You can put the factors in an array, or use separate constants. Something like:

const double factors = {0.166, 0.378, 0.907, ...};
round_to_1_decimal_point(lb * factors[2]) = your weight on venus, rounded to 1 decimal point (the function you'll have to write yourself).

I dont want to be a bother, but. it never showed us how to use doubles. it just told us about them, its a terrible online course. I may just send a complaint about it.

EDIT: Nevermind, never taught us how to write a double function, I THINK. i know what to do. ill just edit the function from the other program xD
 
I few doubts in c those are, All are linux based,

1) How we can convert physical address to virtual adress and vice-versa,for this any linux system call.

2)How a float will store in binary format.

3)what the use of constant volataile.
---------------
rohn
 
Last edited by a moderator:
Back