PDA

View Full Version : What's wrong with my program?



dragos240
March 29th, 2010, 04:39 PM
#include <stdio.h>

int main() {
int a;

printf("How do you feel today? 1 = Good 2 = Bad:\n");
scanf("a");
if (a=1) {
printf("Great!\n");
}
else if (a=2) {
printf("What a pity\n");
}

return 0;
}


Both 1 and 2 will return "Great!". :(

lisati
March 29th, 2010, 04:41 PM
Suggestion: take a look at your "scanf (http://en.wikipedia.org/wiki/Scanf)" statement.

Mmmbopdowedop
March 29th, 2010, 04:42 PM
Meh, probably wrong.

Edited my nonsense. :)

dragos240
March 29th, 2010, 04:42 PM
I did that. It still returned "great."

n0dix
March 29th, 2010, 04:45 PM
try with :

if (a==1) {

Pogeymanz
March 29th, 2010, 04:48 PM
try with :

if (a==1) {

This is the answer.

The double equal sign is how you compare values. The single equal sign sets the left value to the right value.

derrick81787
March 29th, 2010, 04:49 PM
Suggestion: take a look at your "scanf (http://en.wikipedia.org/wiki/Scanf)" statement.

Yeah, this is definitely the issue.

***Edit:
There is also an issue with the "if" statements, too. The way you have it now, it will always print "Great."

swoll1980
March 29th, 2010, 04:49 PM
What does scanf do?

dragos240
March 29th, 2010, 04:52 PM
try with :

if (a==1) {

My now nothing returns when I type an integer. Before they both returned "Great!" now they don't return anything.

RiceMonster
March 29th, 2010, 04:53 PM
== mean equals, whereas = means assignment. Learn the difference between the two.


What does scanf do?

Loads a value from stdin into a variable

lisati
March 29th, 2010, 04:55 PM
try with :

if (a==1) {
Good call. I missed that when I first looked at the program. I think I'm too accustomed to thinking the way some other languages work (e.g. BASIC)

What does scanf do?
It didn't look right when I first looked at the program. Have a look here: http://en.wikipedia.org/wiki/Scanf

RiceMonster
March 29th, 2010, 04:56 PM
Your other problem is that scanf should look like this:


scanf("%d",&a);

Oh, and please indent your code.

dragos240
March 29th, 2010, 04:59 PM
Your other problem is that scanf should look like this:


scanf("%d",&a);Oh, and please indent your code.

Hey! That fixed it! What is the %d for?

derrick81787
March 29th, 2010, 04:59 PM
Your other problem is that scanf should look like this:


scanf("%d",&a);

Oh, and please indent your code.

Yeah, because scanf was wrong, "a" didn't equal 1 or 2. It returned "Great" before because the "if" statements were wrong. After they were fixed, it didn't return either one because "a" didn't equal 1 or 2.

Once scanf is fixed, I think it should work as intended.

- Derrick

n0dix
March 29th, 2010, 05:00 PM
For read a integer.

RiceMonster
March 29th, 2010, 05:00 PM
Hey! That fixed it! What is the %d for?

It means you're reading in an integer.

swoll1980
March 29th, 2010, 05:00 PM
Good call. I missed that when I first looked at the program. I think I'm too accustomed to thinking the way some other languages work (e.g. BASIC)

It didn't look right when I first looked at the program. Have a look here: http://en.wikipedia.org/wiki/Scanf

The only language I know anything about is python. I could tell what was going on though. Everything looked fine to me, except maybe scanf since I didn't know what it did. What language is this?

derrick81787
March 29th, 2010, 05:02 PM
Hey! That fixed it! What is the %d for?

It says that you want an integer. I think of "d" as standing for "decimal" but I don't really know.

The % says that the "d" is a formatting mark and not just a "d" that you want displayed. "%c" will get a character, "%f" will get a float, "%s" will get a string, etc.

You can run man scanf to see the actual parameters it takes, or just look at the wiki that has been linked to. It seems like you need to install a package in order to get the man pages though. The package might be "manpages-dev" but I don't remember for sure.

- Derrick

derrick81787
March 29th, 2010, 05:03 PM
The only language I know anything about is python. I could tell what was going on though. Everything looked fine to me, except maybe scanf since I didn't know what it did. What language is this?

This is C.

wmcbrine
March 29th, 2010, 05:05 PM
For future reference, instead of The Community Cafe, you might want to try Programming Talk (http://ubuntuforums.org/forumdisplay.php?f=39).

chemouna
March 29th, 2010, 05:20 PM
it has 3 problems:
1-if(a==1) not if(a=1):this is an assignment
2-same thing with if(a==2) not if(a=2)
3-scanf("%d", &a)

check out this C tutorial http://beej.us/guide/bgc/output/html/multipage/index.html

MaxIBoy
March 29th, 2010, 06:25 PM
Think of scanf as the exact opposite of printf. Printf takes a format string and arguments, and turns the result into console output.

Scanf takes a format string and pointers to some arguments, and uses that to fill out the arguments' values using console input.

Sprintf takes a format string and arguments, and puts the result in another string for you to do other stuff with. Snprintf is the same but you specify a maximum size (so always use it instead of just sprintf, because otherwise you'll be vulnerable to buffer overrun errors.)

Printf is definitely the easiest to understand of the bunch, although the others are straightforward once you understand printf. So see here for printf:
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

You've done string formatting in Python before right? Like as in this stuff:
http://docs.python.org/release/2.5.2/lib/typesseq-strings.html
It's basically the same thing for C's printf/scanf/sprintf/snprintf.