EvilZone

Programming and Scripting => C - C++ => : gh0st April 13, 2011, 10:41:41 PM

: [C++] if/else exercise
: gh0st April 13, 2011, 10:41:41 PM
well guys I  did this exercise but Im not proud cause I didnt used the statements which Im working on that chapter but however I did it besides the problem didnt mentioned about using loops so  :P

problem:
Write a program that asks the user to type in numbers. After each entry, the program
should report the cumulative sum of the entries to date. The program should terminate
when the user enters 0.


solution:

:
#include <iostream>

using namespace std;

int main()
{
    int x;
   
    cout << "Enter the integrer: ";
    cin >> x;
    cout << endl;
   
    if(x==0)
    {
            cout << "quitting...";
            cout << endl;
            return 0;
}
else
{
    if(x>0)
    {
           x = (x-1)*x;
    cout << x;
    cout << endl;
    system("pause");
}
}
}

so that would be the answer but the topic of the chapter is about loops so I dont know if thats really the answer however I solved it somehow  :P
: Re: [C++] if/else exercise
: ande April 14, 2011, 10:10:16 AM
Your code indent formating is not good my friend. You need to work on that. Also, the code you pasted there wont do what the task asked you to I believe. I see no loop or goto command, so only one integer will be possible to input, m i rit?
: Re: [C++] if/else exercise
: Stackprotector April 14, 2011, 11:18:11 AM
in almost every langauge its if(){ }  else if (){}.
and not  if(){} else{if(){} }.
goodluck
: Re: [C++] if/else exercise
: Xires April 15, 2011, 07:50:34 AM
gh0st; consider researching the 'do-while' loop.  A basic synopsis follows.

You will start the process by requesting integer input from the user and adding it to the current state of the variable(which should probably be initialized to 0 prior to the loop).  Next you will output the current value of the variable and ending the loop with a test on the condition that the variable is, or is not '0'.  That's pretty much the gist of the task.

If you need more help, just ask.

Good luck && have fun.