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
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
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
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/
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
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;
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.
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.