Noob here studying c++ Declaring variables!

hey guys just started my degree and am having quite a bit of trouble, First off i have a quiz which i would really like some help with as im struggling, its like learning a whole new language which is much harder, The first question is ..

1)Write a C++ program that stores the integer value 16 in the variable num1 and the integer value 18 in the variable name num2. (Be sure to declare the variables as integers.) Have your program calculate the total of these numbers and their average. Store the total in an integer variable named total and the average in an integer variable named average. (Use the statement average = total / 2.0; to calculate the average.) Use the cout object to display the total and average.

((And my program code is below, please tell me what i did wrong..))

#include <iostream>
using namespace std;

int main ()
{

int num1,num2;
int total,average;

num1 = 16;
num2 = 18;
total,average;
total = num1 + num2;
average = total/2.0;
cout << "The average of num1 & num2 is " << average << endl;


return 0;

}


2nd Question is (Write a C++ program that displays the following prompts:

Enter the length of the room:
Enter the width of the room:

After each prompt is displayed, your program should use a cin object call to accept data from the keyboard for the displayed prompt. After the width of the room is entered, your program should calculate and display the area of the room. The area displayed should be calculated using the equation area = length * width and should be included in an appropriate message. )

I have no idea where to even start?? I have to know how to do these as i have a quiz on thursday which is marked, so any help would be appreciated, thanks
Just a heads up, you should use the [ code] [/ code] tag for code on here.

The line of code total,average; isn't actually doing anything, so you should remove that.

For the second question, the answer is written in the question. Simple multiplication. :)
area = width * lenght

declare your variables, get input, do the math, output message.
Last edited on
So i just remove total,average; and it will still calculate the total, average ?? Im trying to work out the 2nd quest but i've only just started and have absoulately no idea, All i know is

#include <iostream>
using namespace std;

int main ()

{
Yeah, because the next line in the code calculates the average:
average = total/2.0;

Which should be:
average = total / 2;

seeing as average is declared as an int rather than a float or double.


You should have a read through these tutorials:
http://www.cplusplus.com/doc/tutorial/
Last edited on
well i removed it and it didnt work so my new code is this now which works..

#include <iostream>
using namespace std;

int main ()
{
int num1,num2;
int total;
double average;

num1=16;
num2=18;

total=num1+num2;
average = total/2.0;
cout << " The average of num 1 + num 2 is " << average << endl;
system ("pause")

return 0;
}

As for the secon question i posted can you show me how its done ??
anyone hehe
use somethiing like
1
2
cout << "Enter the length of the room: "
cin >> var_name;

hehe that doesnt quite help, all i know is the basics cacen.. i've been watching video tutorials, reading both my textbooks and still finding it hard hehe, now i know why everyone says programming is hard :D
anyone hehe?
hehe that doesnt quite help, all i know is the basics cacen


Apparently you don't, you can't get much more basic than this. The C++ language tutorial on this site is pretty good, if you still don't understand after reading through it... It's concepts that you need to understand, I am not sure how to teach someone concepts.
http://cplusplus.com/doc/tutorial/
Its easy for the second question.

do like Cacen said.
enter cin >> variable >> other_variable >> endl;

then Cout the same variable, and itll give the the number you gave to it.
Last edited on
this is what i've come up with, am i almost right?

#include <iostream>
using namespace std;

int main ()
{

double length, width, area;

cout << "Enter the length of the room ";
cin >> (not sure what to type here);
area = ( length * width );
cout << "the area of the room is "
<< area << endl ;

return 0 ;

}


I know this is wrong but this is what i created and its the best i can come up with, please correct me what i did wrong and explain what needs to be changed to make it work so i can understand where i went wrong, thanks


double length, width, area;

cout << "Enter the length of the room ";
cin >> (not sure what to type here);


Okay, you have to think a bit here my friend, what TWO variables do you need to get
the area. You wrote it yourself, however, your only getting ONE variable of input( or trying to ).

You declared: double length, width, area; // USE THEM!

cout << "Enter the length of the room ";
cin >> length;

Figure the rest out
@johnny87au
you need two variable right ? length and width to calculate area..
1
2
cout << "Enter the length of the room ";
cin >> (not sure what to type here);


that code should coming twice, one for length and one for width.
This might help. If you want to read user input and store it in a variable, as stated above, you need to use the "cin" stream. The keyword "cin" is used to get input from the user. Here's an example.

1
2
3
4
5
6
int x;

cout << "Enter a number: ";
cin >> x;  // Stores user input in the variable "x". Entering a letter would cause the program to crash.

cout << x << endl;


First of all, we declare an integer variable to store the user input in. Then, we use cout to print a prompt to the screen for the user. Next, you use the cin stream to store the user input in the X variable. If you're getting input, the cin statement must follow the prompt cout statement, but you can put other things in between, such as declaring new variables. Make note of the direction of the arrows. It's very important that they always face like they are above. Then we just use cout to print the value of X to the screen.
Last edited on
The keyword "cin"

cin and cout are global variables, not keywords.
cin and cout are global variables, not keywords.

What? I thought they were objects.
Sorry, correction... yes, cout and cin are objects. cout is an object of the ostream class and cin is an object of the istream class.
Technically, their values are objects, AFAIK, but the distinction is a bit lost in languages like C and C++ because a variable is always bound to the same object.

It's clearer when you do dynamic allocation:

Foo *bar = new Foo;

Here, bar is a variable whose value is a memory address. The object created by new isn't directly bound to any variable.
Topic archived. No new replies allowed.