Is there a reason I can't get this working? (C while loop)

#1metaIsluggPosted 1/25/2013 10:29:37 AM
I'm trying to do a scanf in a while loop. each time it asks for a number, and then outputs the sum of those numbers. so it basically gives you the sum of whatever amount of numbers you want.

however, its not working. it just skips the scanf for some reason


#include <stdio.h>
#include <iostream>

main()
{
int num, resul, times, i;
printf("how many times?:");
scanf("%d",&times);

while (i<times)
{
i=i+1;
printf("number:");
scanf("%d",&num);
resul=resul+num;
}

printf("\n the sum of these numbers is %.2d \n",resul);
}


whats wrong with this code
---
sig
#2PTP2009Posted 1/25/2013 10:57:13 AM
"i" isn't initialized. Set it to 0 before the while loop.
#3DujasPosted 1/25/2013 11:10:59 AM
For that matter, neither is resul. Just curious, why drop the t? It's one letter.
#4metaIslugg(Topic Creator)Posted 1/25/2013 3:54:54 PM
From: PTP2009 | #002
"i" isn't initialized. Set it to 0 before the while loop.


Wow, you're right. I thought any variable was 0 by default. In fact... I've been doing all my code based on that, and hadn't had any problems til now. Weird.

From: Dujas | #003
For that matter, neither is resul. Just curious, why drop the t? It's one letter.


Haha I guess so. However, I'm doing these in portuguese (I just translated for the sake of this board), and so the word is actually a bit longer when written in portuguese.

Thank you for the help!
---
sig
#5neoXPosted 1/25/2013 9:17:54 PM
In general you want to always variables when you declare them.

int i = 0, j = 0, k = 0;

or

int i = 0;
int j = 0;
int k = 0;

or

int i = int(0);
int j = int(0);
int k = int(0);

or

int i(0);
int j(0);
int k(0);


Which ever you find easiest to understand. See this for an example: http://ideone.com/0qxybU

You're using C++, not C (you've included iostream). I highly recommend using cout and cin.
---
http://www.goo.gl/en6H3
#6BewmHedshotPosted 1/25/2013 11:17:27 PM
neoX posted...
You're using C++, not C (you've included iostream). I highly recommend using cout and cin.

Don't listen to this recommendation, tc.

Learn C, drop that iostream nonsense. Knowing how to write idiomatic C (that is, writing C how C programmers do it) is a great benefit to a programmer even if he doesn't work in C; while writing C-style C++ is likely to get you sent home from an interview early.
---
Money may not buy happiness, but I'll take my ****ing chances.
#7ReconditePhreakPosted 1/26/2013 10:56:25 AM
C and C++ are two different languages, learn both and ignore anyone who is unduly biased in either direction (absolutely nothing wrong with iostream and friends).

But always remember, one of the things that makes sure C++ will never go away is how low the barrier between C and C++ is. There isn't another language in existence that has it easier besides arguably Objective-C, and you won't find it much outside of the Mac world.

There are no popular mainstream languages that I know of that cannot talk to C in some fashion. If you can talk to C, you can talk to everything else by using C in the middle.

And C++ does it better. And your C knowledge is useful everywhere.

Learn both. But not at the same time.
---
Believes the individuals who report to moderators wish they had more control than they do.
#8ReconditePhreakPosted 1/26/2013 10:59:35 AM
Also, in general, C++ doesn't make you pay for what you don't use. If you don't manually initialize the variable, C++ doesn't do it for you.

This is different from other languages such as Java, et al, that do default initialize.



int i();

^ note the "constructor syntax", this will default initialize, which means initialize to 0. I recommend you explicitly initialize it to 0, but you can use this syntax as well. Most C++ developers should be able to recognize this, but it's more idiomatic to use "= 0".

just an fyi.
---
Believes the individuals who report to moderators wish they had more control than they do.
#9neoXPosted 1/26/2013 4:05:17 PM
Bah I forgot the default constructor

:p
---
http://www.goo.gl/en6H3