This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++] if/else exercise  (Read 5901 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++] if/else exercise
« on: 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:

Code: [Select]
#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
« Last Edit: April 13, 2011, 10:42:21 PM by gh0st »

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: [C++] if/else exercise
« Reply #1 on: 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?
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [C++] if/else exercise
« Reply #2 on: April 14, 2011, 11:18:11 AM »
in almost every langauge its if(){ }  else if (){}.
and not  if(){} else{if(){} }.
goodluck
~Factionwars

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C++] if/else exercise
« Reply #3 on: 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.
-Xires

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.