Need help writing a code..

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
#include <stdio.h>

int main(void)
{
printf("Please Enter Pin Number");
scanf("pin=1234");
if ("pin=1234") {
printf("Correct Pin");
} else {
printf("Incorrect Pin");
}
return 0;
}

I'm taking a programming course for the first time as part of engineering, and I have no idea what to do here. I got this far, but how do I make it only type Correct Pin when 1234 is entered? When I run the program..it says Correct Pin for ANY number entered.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
scanf("pin=1234"); what do you think this does?
if ("pin=1234") { what do you think this does?

int foo;
scanf("%d", &foo);
if (foo == 1234) {
// stuff
}
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: CTho9305
scanf("pin=1234"); what do you think this does?
if ("pin=1234") { what do you think this does?

int foo;
scanf("%d", &foo);
if (foo == 1234) {
// stuff
}

Like I said, I don't know much, doesn't scanf look for pin=1234 to continue the program? and if (pin=1234)...isn't that the statement for the next step?
 

talyn00

Golden Member
Oct 18, 2003
1,666
0
0
Originally posted by: mOeeOm
Originally posted by: CTho9305
scanf("pin=1234"); what do you think this does?
if ("pin=1234") { what do you think this does?

int foo;
scanf("%d", &foo);
if (foo == 1234) {
// stuff
}

Like I said, I don't know much, doesn't scanf look for pin=1234 to continue the program? and if (pin=1234)...isn't that the statement for the next step?

Use a do while loop if you want to keep prompting until you get the correct response.
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Um, the code below the empty line is where CTho9305, nice enough, gave you the solution.

I hope you understand the concept of variables.
Scanf is a command that reads input and saves it in a varibale.

int foo; --> this initializes the variable as an integer (entire numbers) Don't call it foo, though, give it a name that means something (like pin).

scanf("%d", &foo); --> scanf is now being told to look for an integer value (%d), and save it in foo. the & pass the address of the variable instead of the variable.

if (foo == 1234) { --> You used pin = 1234, which just assignes 1234 to pin, and is always true (your pin would always be "correct" no matter what you enter. pin == 1234 means that you want to compare those two.
// stuff
}
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Use a while loop if you want to keep prompting until you get the correct response.
Suggesting "while" to someone who doesn't yet understand "if" is probably not going to be useful.
 

talyn00

Golden Member
Oct 18, 2003
1,666
0
0
Originally posted by: CTho9305
Use a while loop if you want to keep prompting until you get the correct response.
Suggesting "while" to someone who doesn't yet understand "if" is probably not going to be useful.

true, considering any type of loop relies on conditionals....
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
if you put :

if (var = 1)

It will end up setting the variable to 1. The correct way is with two equal signs.

if (var == 1)

That will check if the variable is 1.

Regardless, I misread it in the first place. If ("var=number") doesn't mean anything. :) It should return 1 all the time, explaining the behavior of your program.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
While looking for information so I could figure out this problem (it's fun to mess around with this programming stuff when you know next to nothing ;)), I came across this site. Some of the information in there was pretty good.

HTH.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
#include <stdio.h>

int pin;

int main(void)
{
printf("Please enter pin number");
scanf("%d", &pin);
if (pin == 1234) {
printf("You have entered a correct Pin number");
} else {
printf("You have entered an incorrect pin number");
}
return 0;
}

Ok it works now, but...now I need to make it branch off, after they enter the correct pin number, how do I make it branch off into 3 options, like: Withdrawl, deposit and transfer while keeping the if else statement?
 

Atheus

Diamond Member
Jun 7, 2005
7,313
2
0
I suggest you sit around with people from your course and work this out rather than asking a bunch of experienced coders. You will learn more that way.

I'll give you a hint though -



#include <stdio.h>

int pin;

int main(void)
{
printf("Please enter pin number");
scanf("%d", &pin);
if (pin == 1234) {
printf("You have entered a correct Pin number");
} else {
printf("You have entered an incorrect pin number");
}


// print out the various options, withdrawl, deposit and transfer, and ask the user to choose

// get some input from the user

// if the user requested withdrawal, jump to withdrawal function, etc


return 0;
}






 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Its due in 2 days, and I don't know people in the course, so..this is the next best thing :)

What I'm wondering, after they put in the correct pin, how do I make it go into the 3 options while still having the if else option once they enter the wrong pin. I don't know how to do this...
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Whoops, sorry, I did indeed misunderstand your last post. But I'll keep it here if you care to ever do this. Can you please clarify your question? What precisely do you want to have your program do? 'Go into 3 options' isn't specific enough. :)

Originally posted by: mOeeOm
Its due in 2 days, and I don't know people in the course, so..this is the next best thing :)

What I'm wondering, after they put in the correct pin, how do I make it go into the 3 options while still having the if else option once they enter the wrong pin. I don't know how to do this...

3 options meaning 2 valid PINs and everything else invalid?

Well right now you check the input against one of the correct PINs. If it's valid, you display correct, if it's invalid, you display incorrect. So now that you have a second one, you must change the invalid clause to encompass another PIN, right? That's where else if comes in. This should make sense.

There's another way to do this. You can use the logical OR operator (|| symbol) and omit the else if altogether.
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Ok I want the program to do this, say they enter 1234, it will ask them would you like to make a withdrawl, deposit or transfer and I can build on this further. And if they don't enter the 1234, it says you entered a wrong pin.
 

helpme

Diamond Member
Feb 6, 2000
3,090
0
0
You can use more if else statements, or use a case statement. Look up an example of case/switch statements, they're a bit easier to work with than a bunch of ifs.