Trouble with a C++ Program

Started by
32 comments, last by M2tM 13 years, 6 months ago
Thank you everyone for the responses, it is greatly appreciated.
Advertisement
Quote:Original post by M2tM
Google these:

int
for
if
std::cin
std::cout

The general pseudo-code for your program might look like this:

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS
//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
//LOOP END
//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT

You need to learn how to break problems down into discreet steps like this if you plan on programming at all. Then what you do is take these steps and convert them into code. If you can do this in your head, that's great. If you cannot then you will want to write some pseudo-code or a flowchart to help understand the problem.

As far as I can tell the problem does not warrant an array unless you are also expected to output the numbers individually later on. You can keep a running total in the input loop and avoid the need for a vector (or array). Maybe I'm missing something though.

If it helps, you can try copy/pasting my pseudo code into your editor and directly translating from English into C++ deleting the comments one at a time as you do so.


Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?
Moved to For Beginners.

Quote:Original post by Blckknight118
Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


No, because you don't need to "hold" them all.

You are going to have some sort of loop that reads each number from the standard input. Each time, you read it into the same variable, check the sign of the value, and update the appropriate running totals.

Think of how you would do it if you had to do the math yourself, in your head, as someone reads the numbers off to you, one at a time. Would you bother to remember what numbers were given to you previously? No; you have enough to think about as it is. Instead you would just keep track of the sums and the most recent number, so that you can do the addition.
Quote:Original post by Blckknight118
Quote:Original post by M2tM
Google these:

int
for
if
std::cin
std::cout

The general pseudo-code for your program might look like this:

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS
//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
//LOOP END
//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT

You need to learn how to break problems down into discreet steps like this if you plan on programming at all. Then what you do is take these steps and convert them into code. If you can do this in your head, that's great. If you cannot then you will want to write some pseudo-code or a flowchart to help understand the problem.

As far as I can tell the problem does not warrant an array unless you are also expected to output the numbers individually later on. You can keep a running total in the input loop and avoid the need for a vector (or array). Maybe I'm missing something though.

If it helps, you can try copy/pasting my pseudo code into your editor and directly translating from English into C++ deleting the comments one at a time as you do so.


Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


An array is a collection of variables. Typically in C++ you use std::vector instead of bare arrays... Why would you need to store a single number in several different variables?

Try walking through the pseudo-code as if you're actually running the program.

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS

Here we've just created 3 variables of type int and set them to 0 to store the grand totals.

//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 2

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)0 + 2 = 2.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the number is greater than 0, so our "TOTAL OF POSITIVE NUMBERS" is 0 + 2 = 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
This is not true for 2 and so "TOTAL OF NEGATIVE NUMBERS" stays the same.

//LOOP END
Ok, so we hit the loop end, but because we want to do this 10 times we'll go back to the first step in the instruction (I'm copying and pasting the inner part of the loop again):

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter -5 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)2 + -5 = -3.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
No, the new number is not greater than 0 and so our "POSITIVE NUMBERS TOTAL" stays at 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
Yes, our temporary number is less than 0 and so we get "NEGATIVE NUMBER TOTAL"(0) + -5 = -5


//LOOP END
Ok, we're at the end of the loop again, we'll do one more run through in this example:

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 12 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)-3 + 12 = 9.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the new number is greater than zero, so we add it to our "POSITIVE NUMBER TOTAL"(2) + 12 = 14

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
No, this is not true so we don't do anything and the value still holds -5.


//LOOP END
Ok, I'm done with my example, but imagine we do this 7 more times. Now we exit the loop.



//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT
Alright, this is the last step in the pseudo code, so we output:
"Positive Number Total = 14, Negative Number Total = -5, Total of All Numbers = 9"
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Quote:Original post by Zahlman
Moved to For Beginners.

Quote:Original post by Blckknight118
Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


No, because you don't need to "hold" them all.

You are going to have some sort of loop that reads each number from the standard input. Each time, you read it into the same variable, check the sign of the value, and update the appropriate running totals.

Think of how you would do it if you had to do the math yourself, in your head, as someone reads the numbers off to you, one at a time. Would you bother to remember what numbers were given to you previously? No; you have enough to think about as it is. Instead you would just keep track of the sums and the most recent number, so that you can do the addition.


But the program needs ten different values from the user. Aren't I supposed to initialize ten separate int variables to be manipulated. How can the user enter in ten numbers if none of them are stored? Sorry, I'm just really confused with this. I understand how a for loop works, I just don't understand how it does in a situation where the user inputs ten separate numbers when you can only use one variable in the statement of the loop.
Quote:Original post by M2tM
Quote:Original post by Blckknight118
Quote:Original post by M2tM
Google these:

int
for
if
std::cin
std::cout

The general pseudo-code for your program might look like this:

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS
//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
//LOOP END
//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT

You need to learn how to break problems down into discreet steps like this if you plan on programming at all. Then what you do is take these steps and convert them into code. If you can do this in your head, that's great. If you cannot then you will want to write some pseudo-code or a flowchart to help understand the problem.

As far as I can tell the problem does not warrant an array unless you are also expected to output the numbers individually later on. You can keep a running total in the input loop and avoid the need for a vector (or array). Maybe I'm missing something though.

If it helps, you can try copy/pasting my pseudo code into your editor and directly translating from English into C++ deleting the comments one at a time as you do so.


Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


An array is a collection of variables. Typically in C++ you use std::vector instead of bare arrays... Why would you need to store a single number in several different variables?

Try walking through the pseudo-code as if you're actually running the program.

//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL POSITIVE NUMBERS
//DECLARE A VARIABLE TO HOLD THE TOTAL OF ALL NEGATIVE NUMBERS

Here we've just created 3 variables of type int and set them to 0 to store the grand totals.

//LOOP OVER THE FOLLOWING 10 TIMES
////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 2

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)0 + 2 = 2.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the number is greater than 0, so our "TOTAL OF POSITIVE NUMBERS" is 0 + 2 = 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
This is not true for 2 and so "TOTAL OF NEGATIVE NUMBERS" stays the same.

//LOOP END
Ok, so we hit the loop end, but because we want to do this 10 times we'll go back to the first step in the instruction (I'm copying and pasting the inner part of the loop again):

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter -5 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)2 + -5 = -3.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
No, the new number is not greater than 0 and so our "POSITIVE NUMBERS TOTAL" stays at 2

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
Yes, our temporary number is less than 0 and so we get "NEGATIVE NUMBER TOTAL"(0) + -5 = -5


//LOOP END
Ok, we're at the end of the loop again, we'll do one more run through in this example:

////USE CIN TO READ INPUT INTO A TEMPORARY INTEGER
Let's say we enter 12 this time

////ADD THE TEMPORARY INTEGER TO THE TOTAL OF ALL NUMBERS
Ok, so our "TOTAL OF ALL NUMBERS" is (original value)-3 + 12 = 9.

////IF THE TEMPORARY INTEGER IS GREATER THAN 0 ADD IT TO THE POSITIVE NUMBERS TOTAL
Yes, the new number is greater than zero, so we add it to our "POSITIVE NUMBER TOTAL"(2) + 12 = 14

////IF THE TEMPORARY INTEGER IS LESS THAN 0 ADD IT TO THE NEGATIVE NUMBERS TOTAL
No, this is not true so we don't do anything and the value still holds -5.


//LOOP END
Ok, I'm done with my example, but imagine we do this 7 more times. Now we exit the loop.



//USE COUT TO OUTPUT THE TOTAL VARIABLES AND SOME DESCRIPTIVE TEXT
Alright, this is the last step in the pseudo code, so we output:
"Positive Number Total = 14, Negative Number Total = -5, Total of All Numbers = 9"


OMG THAT MAKES SO MUCH DAMN SENSE.

Thank you so much, that just cleared so much, everything clicked. I don't know how to thank you.
Quote:Original post by Blckknight118
Quote:Original post by Zahlman
Moved to For Beginners.

Quote:Original post by Blckknight118
Also, wouldn't you need to implement arrays to hold all of the total numbers, total negative numbers and total positive numbers?


No, because you don't need to "hold" them all.

You are going to have some sort of loop that reads each number from the standard input. Each time, you read it into the same variable, check the sign of the value, and update the appropriate running totals.

Think of how you would do it if you had to do the math yourself, in your head, as someone reads the numbers off to you, one at a time. Would you bother to remember what numbers were given to you previously? No; you have enough to think about as it is. Instead you would just keep track of the sums and the most recent number, so that you can do the addition.


But the program needs ten different values from the user. Aren't I supposed to initialize ten separate int variables to be manipulated. How can the user enter in ten numbers if none of them are stored? Sorry, I'm just really confused with this. I understand how a for loop works, I just don't understand how it does in a situation where the user inputs ten separate numbers when you can only use one variable in the statement of the loop.


Ok, let's try this even a step slower.

You have a variable, why is it called a variable? Because it can change!

What does the following program do?

int x;
x = 2;
cout << "The variable contains: " << x << endl;
x = 5;
cout << "The variable contains: " << x << endl;
x = 20;
cout << "The variable contains: " << x << endl;
x = 5;
cout << "The variable contains: " << x << endl;

Assuming you have this within your main function this should output:
The variable contains: 2
The variable contains: 5
The variable contains: 20
The variable contains: 5

What if we do this:

int x;
cin >> x;
cout << "You just typed: " << x << endl;
cin >> x;
cout << "You just typed: " << x << endl;
cin >> x;
cout << "You just typed: " << x << endl;
cin >> x;
cout << "You just typed: " << x << endl;

We are using the same variable, but we've asked the user to enter 4 numbers. We don't need a different variable for each number because we only need to use that number for a single line after, we can then re-assign x later on and re-use the variable.

Alright, let's try something else now, previous example:

int x;
int Total = 0;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;
cin >> x;
Total+=x;
cout << "You just typed: " << x << " The running total is: " << Total << endl;

That's cool, but we're doing the exact same thing several times, we can put that into a for loop!

Your assignment now is to take the code I wrote above and put it into a for loop that runs 10 times. Then turn that into the assignment you had.
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Quote:Original post by Blckknight118
Quote:Original post by M2tM
...


OMG THAT MAKES SO MUCH DAMN SENSE.

Thank you so much, that just cleared so much, everything clicked. I don't know how to thank you.


No problem, I'm glad to help :D
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Ok so I thought I had and I understand, but I don't know how to implement it.

Here's my code thus far.

#include <iostream>
using namespace std;

int main() {


cout << " Welcome to Whole Number Extravagenza! \n";
cout << " Please enter a number 10 numbers to be calculated ";
cout << " Pleae enter each number followed by a space ";

int num_totalall;
int num_negativetotal;
int num_positivetotal;

int num;

cout << " Please enter in 10 numbers ";
This is where I get tripped up. What about all ten of the values that are to be entered in? How are the supposed to be tested to see if they are greater than or less than zero?
for(num = 0; num > 10; num++) Here I try to get the loop to go ten times
{ I don't know what to put here.. Would it be num+=num_total?
Quote:Original post by Blckknight118
Ok so I thought I had and I understand, but I don't know how to implement it.

Here's my code thus far.

#include <iostream>
using namespace std;

int main() {


cout << " Welcome to Whole Number Extravagenza! \n";
cout << " Please enter a number 10 numbers to be calculated ";
cout << " Pleae enter each number followed by a space ";

int num_totalall;
int num_negativetotal;
int num_positivetotal;

int num;

cout << " Please enter in 10 numbers ";
This is where I get tripped up. What about all ten of the values that are to be entered in? How are the supposed to be tested to see if they are greater than or less than zero?
for(num = 0; num > 10; num++) Here I try to get the loop to go ten times
{ I don't know what to put here.. Would it be num+=num_total?
First, you should initialize your variables to a known value.

Second, you need to collect the data from the user. num is not data collected from the user. You need a cin in there somewhere. To be clear, you need 5 variables (3 to hold the totals, 1 to hold the data you get from the user, and a final one to maintain your position in the for loop).

Third, once you collect data from the user you want to check whether that data matches the necessary condition. If the data matches the necessary condition for a running total then you'll want to add it to the running total. Otherwise do not add it to the running total.

Hope this helps.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement