Need Help with my c++ programming class.

I need assistance in understanding c++ programming. I'm in a class right now, and I do not understand anything. I feel like nothing is being explained how it should.


I do not want answers to any homework, just explanations on how to understand what is going on.

2. What is the output from the following C++ code fragment?

int num = 1;
while(num < 10)
{
cout << num << " ";
num += 2;
}
cout << end;


How do I solve this? When inputted in Microsoft Visual, the program won't run. Please do not give the answers, just explain how to read it.

6. What is the output from the following C++ code fragment if the following values are the inputs to cin?

58 23 75 176 145 -999

int num;
cin >> num;
while(num != -999)
{
cout << num % 25 << " ";
cin >> num;
}
cout << endl;


This one I don't understand. ( I really don't understand anything). Please help. Again No answers please, just explain how to read this.

Alicia


Welcome to the C++ forums. Please use the code tags when pasting code in the future.

For your first problem, the while loop always checks the condition before executing the instructions within its block.

cout and endl are undefined. I suggest to put in the line #include <iostream> before the main function. Also, you may want to use the std:: prefix before calling the cin, cout, and endl functions, like so:
std::cout << num << std::endl;
That still does not help me. These codes were supplied by my professor. I need to be able to understand the work before I run it. Please help me. Thanks
Alicia, you wouldn't happen to be a Math Major that is forced to take a C++ class, would you? lol

In all seriousness, the answers to your questions are simple. You must however, learn to compile and run code yourself.

There are a few requirements you need to fulfill in order to get code to compile.

You first need a main function. This function will run your program and when it is finished, your program will end. That is all you need to know for now.

You also need to include certain libraries to use throughout your code. These libraries contain precoded and compiled objects and functions that you can use to your benefit without having to code them yourself.

cin and cout require #include <iostream> like Nexius stated.

Instead of typing std:cin or std:cout you can simply include using namepace std; at the top of your code. If you really want to know the difference you can easily research that on your own but I wouldn't worry about it now.

This is how your code could if you want to run them as 2 separate programs. You don't have to return an int to main but it is the most traditional way.

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

using namespace std;

int main()
{
    int num = 1; 
    
    while (num < 10)
    { 
         cout << num << " ";
         num += 2;
     }
     cout << end;

     return 0;
}


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

using namespace std;

int main()
{
    int num;
    cin >> num;

    while (num != -999)
    {
         cout << num % 25 << " ";
         cin >> num;
     }
     cout << endl;

     return 0;
}

That still does not help me. These codes were supplied by my professor. I need to be able to understand the work before I run it. Please help me. Thanks


You will understand how they work BY running them. This is all very trivial stuff and is the perfect time to learn by doing.

It isn't until you reach Object Oriented programming or Design Patterns where you actually do need to stop and learn before you start playing around. ATtleast imo.


EDIT - If you are using Visual Studio you might want to learn how to use the debugger. If you click on a line of code you can press Ctrl+F9 or click to the very left of it to set a breakpoint. Then, if you press F5 to run the code it will stop at that breakpoint and you can hover over variables to see what is stored in them at runtime. You can even enter in new values or statements to unit test certain loops or if statements.

The more simple but brute force methods is to code cout statements with the variable to output it's value to the screen.

Good luck.
Last edited on
Hi..

If your professor supplied the code as you posted it, i don't think its intended for you to enter in a compiler and run it. (then you would have to correct it)
I suspect you are supposed to come up with the answers by studying the code on the basis of what you have probably already heard in class ?

So something like this:


1
2
3
4
5
6
7
8
int num;                               // num is declared
cin >> num;                         // user input assigned to num
while(num != -999)              // as long as num is not equal -999
{
cout << num % 25 << " ";  // output the remainder of integer division of num/25
cin >> num;                         // get a new input
}
cout << endl;                       // now -999 was input. 


so... the code returns the remainder of an integeer division, until the user inputs -999. Then you can just do the divisions in your head or on paper.

Have fun
closed account (o3hC5Di1)
Hi there,

Although there's already quite good answers here, I thought I'd give you the jnfo on what all the statements used actually mean, to help you understand. The code won't run as it is for various reasons, but running the code is not the objective, it's understanding it. So here goes:

As for the first example:

1
2
3
4
5
6
7
int num = 1;
while(num < 10)
{
cout << num << " ";
num += 2;
}
cout << end;


int num = 1; This defines a variable with the name "num" as an integer value. Integer is the datatype used for non-floating point numbers. We need to tell c++ of which type a variable is, so it knows how much space it needs to keep free in the computers memory to store the value.

while(num < 10) This starts a loop, more specifically a while-loop. Loops are used to repeat certain actions for an arbitrary amount of times and the amount of repetitions is usually determined by an expression, in this case num < 10 This line could be read as "As long as the "num" variable's value is smaller than 10 (so not equal to ten!), keep doing the following:". The code that has to be repeated is placed inside curly brackets and called a code block:

1
2
3
4
{
cout << num << " ";
num += 2;
}


So, as long as "num" is smaller than ten, this code will be executed. cout is a statement that refers to "standard output", usually your screen. This is usually followed by the << operator, also known as the insertion operator. cout << can be read as "send following to the screen". What comes after will be written onto the console, in this case the "num" variable and a blank space. In between another << is put, to let c++ clearly see the difference between the variable and the space. After that, we need to make sure that we increase the value of the num variable, otherwise it will always be smaller than ten, and the while-loop will keep running forever. num += 2; Does exactly that. the += operator is the same as saying "take the current value of num, and add (number) to it". In other words, increase the current value of num by the number following the operator.

cout << end;
cout << you already know now, and endl is what is called a manipulator. Manipulators are used to change the forrmat of the data you're sending to the screen. In the case of endl that means adding a linebreak at the position where it's called.

So, the output of this program will be the value of the variable num, for as long as the while() loop runs. It's best to take a bit of paper and write down every step of the loop, keeping track of the values.

1
2
3
4
5
6
7
8
int num;
cin >> num;
while(num != -999)
{
cout << num % 25 << " ";
cin >> num;
}
cout << endl;


int num;
This is similar to the first statement above, but it does not assign a value to num immediately. It's like telling c++ "you may come across a number that goes by the name of num, so now you know" Otherwise c++ would tell you it doesn't know that variable if you would call it in the program.

cin >> num;
This ia again similar to the cout << statements, but here we have cin, which is a statement referring to "get input from the keyboard", followed by the insertion operator >>. The statement can be read as "take input from the keyboard and put the value into the variable called num".

while(num != -999)
While-loop again, but with a different expression this time. The != operator mean "not equal to", so the statement says "do the following while the value of num is not equal to minus 999".

1
2
3
4
{
cout << num % 25 << " ";
cin >> num;
}


cout you know now, but % is different. % is called the modulus operator and will return the remainder of the division between the number on the left and the number on its right. For instance, 3%2 is 1. this is again followed by a blank space. After that, there is another cin statement, again asking the user to input a number through the keyboard and putting that input into the num variable.

cout << endl; is the same as above.

That should get you on your way at least, I hope the explanation makes sense.
For further information, check out this websites great tutorial: here : http://cplusplus.com/doc/tutorial/ and do get back to us if you have any more questions.

All the best,
NwN
These are some great answers, but if you want to learn c++ you will need to pick up the general basics of the syntax, I learned basic syntax while I was using Lua so I don't know how you could start learning it with C++, there are some video tutorials on youtube like "anti-rtfm spoon fed c++" and "bostons c++ tutorials" just be aware that while bostons videos sometimes explain things better, he has a lot of bad habits you shouldn't be using.
Topic archived. No new replies allowed.