d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Simple Task > C++
Add Reply New Topic New Poll
Member
Posts: 17,242
Joined: Jun 25 2010
Gold: 0.00
Oct 20 2014 02:49am
I've been using c++ for only about 2 months, so I'm still at very basics

I need to make a simple program that asks for number until correct number has been inserted and then counts the sum of all the inserted numbers but got no idea how to implement the sum in there :(

so far i have this

Code
#include<iostream>
using namespace std;

int main(){
const int number = 0;
int guess;

cout << "Guess a number" << endl;
cin >> guess;
while (guess != number){
cout << "Guess again" << endl;
cin >> guess;
}
cout << "You guessed right, the sum of your guesses is: " << << endl;
system("pause");
return 1;
}


so for example if you first answer 1, then 2, then 0, it would give a sum of 1 and 2

any help is appreciated :P

This post was edited by Zoni on Oct 20 2014 02:58am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Oct 20 2014 05:02am
I'm thinking of a few ways you could keep track of the sum, you could create a counter, then add each guess to this, then add the final guess at the end.

You could do something more complicated and harder too, like keep an array of all the guesses, then calculate the sum of everything at the end.

Some Python I tried for this, may as well be pseudo-code, I tried my first idea out.

Code
from random import randint


guess_me = randint(0,5)
prompt = 'Enter number between 0-5: '


def askii(prompt, retries=6):
global guess_me
shalcp = guess_me

count = 0

while retries>0:
try:
number = input(prompt)

if int(number) == shalcp:
print("guess:"+str(shalcp))
count = shalcp + count
print("sum:"+str(count))
retries = 0

elif number != shalcp:
count += int(number)
retries = retries-1

if retries == 0:
print('somehow, you lost...')

else:
number
continue

except ValueError:
print('zzz, stop that!')
continue


if __name__ == "__main__":
askii(prompt)


Sample output, it is Python 3 by the way, only computer I use this crap on

Code
C:\>python -i wow.py
Enter number between 0-5: 6
Enter number between 0-5: 5
Enter number between 0-5: 4
Enter number between 0-5: 3
Enter number between 0-5: 2
guess:2
sum:20
>>>


Tried it with lists too, because why not, plus Python has a sum function built into stdlib

Code
from random import randint


guess_me = randint(0,5)
prompt = 'Enter number between 0-5: '


def askii(prompt, retries=6):
global guess_me
shalcp = guess_me

count = []

while retries>0:
try:
number = input(prompt)

if int(number) == shalcp:
print("guess:"+str(shalcp))
count.append(int(shalcp))
print("sum:"+str(sum(count)))
retries = 0

elif number != shalcp:
count.append(int(number))
retries = retries-1

if retries == 0:
print('somehow, you lost...')

else:
number
continue

except ValueError:
print('zzz, stop that!')
continue


if __name__ == "__main__":
askii(prompt)


Sample:

Code
C:\>python -i wow.py
Enter number between 0-5: 5
Enter number between 0-5: 3
Enter number between 0-5: 2
Enter number between 0-5: 4
guess:4
sum:14
>>>


This post was edited by killg0re on Oct 20 2014 05:26am
Member
Posts: 17,242
Joined: Jun 25 2010
Gold: 0.00
Oct 20 2014 06:06am
i really appreciate the effort you put in here, but i don't understand python at all (or any other, really) :(

i just recently started studying IT in the university and had never used any programming language before

you mentioned a sort of counter should work, is that the simplest way to work around or is there some more simple way? i mean, the simpler the better for me at this level ^_^

i tried some googling yesterday but couldn't find anything to use. probably because i wasn't quite sure what keywords to use :D so even pointing to guide a something using this type of stuff would be much appreciated
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Oct 20 2014 06:10am
Quote (Zoni @ Oct 20 2014 06:06am)
i really appreciate the effort you put in here, but i don't understand python at all (or any other, really) :(

i just recently started studying IT in the university and had never used any programming language before

you mentioned a sort of counter should work, is that the simplest way to work around or is there some more simple way? i mean, the simpler the better for me at this level ^_^

i tried some googling yesterday but couldn't find anything to use. probably because i wasn't quite sure what keywords to use  :D  so even pointing to guide a something using this type of stuff would be much appreciated


Create

int counter

Start it at 0, of course, every time you make a guess increment it by the value of the guess

counter = counter + guess or counter += guess

That is essentially what I did in the first example, the logic is the same at least.

http://www.cplusplus.com/doc/tutorial/operators/

This post was edited by killg0re on Oct 20 2014 06:14am
Member
Posts: 17,242
Joined: Jun 25 2010
Gold: 0.00
Oct 20 2014 07:11am
Quote (killg0re @ 20 Oct 2014 14:10)
Create

int counter

Start it at 0, of course, every time you make a guess increment it by the value of the guess

counter = counter + guess or counter += guess

That is essentially what I did in the first example, the logic is the same at least.

http://www.cplusplus.com/doc/tutorial/operators/


thanks a lot, got it working! it's not the cleanest code but will do for me :)

Code
#include<iostream>
using namespace std;

int main(){
const int number = 0;
int guess;
int counter = 0;
cout << "Guess a number" << endl;
cin >> guess;
counter = counter + guess;
while (guess != number){
cout << "Guess again" << endl;
cin >> guess;
if (guess != number){
counter = counter + guess;
}
}
cout << "Arvasit oikein, arvaustesi summa on: " << counter << endl;
system("pause");
return 1;
}
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Oct 20 2014 10:21pm
Should probably use a do... while loop instead of a while, this would avoid some code repetition (which is bad)
Member
Posts: 17,242
Joined: Jun 25 2010
Gold: 0.00
Oct 21 2014 03:01am
Quote (m0hawk @ 21 Oct 2014 06:21)
Should probably use a do... while loop instead of a while, this would avoid some code repetition (which is bad)


i thought do - while is the same thing as while. arent they?

i mean while loop was introduced to me like a weed ago so i dont know :P
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Oct 21 2014 04:59am
do - while always executes at least once, which is exactly what you want here.
Member
Posts: 17,242
Joined: Jun 25 2010
Gold: 0.00
Oct 21 2014 05:53am
Quote (KrzaQ2 @ 21 Oct 2014 12:59)
do - while always executes at least once, which is exactly what you want here.


if i used do - while in this, guessed it right on the first try, i wouldnt want to it do the the do - while loop, right? or did i miss something?
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Oct 21 2014 06:12am
That depends, but its possible. If you don't want to display the sum in case of first guess being correct, then yeah, do - while isn't what you want.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll