-2

hi we have this homework which involves an input of a username and password. i figured i should use string because there are letters involved. i was playing around the commands to try and figure out how to work out my homework.my question is, how do i get this part to work, because my conditional statements doesnt seem to work. here's my program. it's supposed to print "2" when i input the correct username. but it doesnt seem to enter the if statement

 #include <stdio.h>
 #include <conio.h>
 #include <stdlib.h>
 #include<string.h>
 #define p printf
 #define s scanf



main(){

       char pin[5]="1234",user[20]="divina",npin[5],npin2[5],iuser[20],ipin[5];
       float wdraw,bal=5000,dep;

       p("Welcome to the ATM program.");
       p("\nPlease enter your username and password.");
       p("\nusername: ");
       s("%s",&iuser);
       if(iuser==user){
        p("2");
       }

       getch();
       }
joseph
  • 1
  • if you define single letters for all the variables and literal strings, you can shorten the main function down to < 100 characters! – thang Sep 28 '14 at 03:08

2 Answers2

1

Hi array comparison will always return false so i eddited your code and now it works like a charm

#include <stdio.h>
#include "iostream"
#include <stdlib.h>
#include<string>

#define p printf
#define s scanf

using namespace std;

int main(){

std::string pin="1234";
std::string user="divina";
std::string npin,npin2,iuser,ipin;
float wdraw,bal=5000,dep;

p("Welcome to the ATM program.");
p("\nPlease enter your username and password.");
p("\nusername: ");
cin>>iuser;
if(iuser==user){
    p("2");
}


}

hope this helps

MrAbdul
  • 76
  • 3
  • 11
  • hi thanks for the help.i gotta ask though, what is std::string? i mean obviously it's how you declared the variables but our teacher hasnt taught that to us.can you tell me how it works please? – joseph Sep 28 '14 at 03:19
  • std means standard so in this case i am just declaring a pin variable and it is a string and it uses standard namespace, it's weird that your teacher didn't explain std usually it is the first thing to be used, non the less you don't need to put std in every time you declare a variable because i already said using namespace std which tells the compiler that all name spaces are standard – MrAbdul Sep 28 '14 at 04:02
0

The basic problem here is that you must compare the string contents, which you're not doing. The condition inside the if doesn't do that. Instead, it just compares the variables iuser and user.

Since those are arrays, what those means actually are memory references, or pointers. Refering to array names only will give you a number, which is the address of memory where the actual content begins, but NOT the real string contents. So, what the if actually does is verifying that those two arrays are at the very same place in memory (which they will never do) instead of comparing the real contents as you intend. Hope that's clear enough :D. I don't know if the course already touched pointers things.

So, what's the real solution? Do a string comparision. There is a C function that does preciely that, strcmp. In short, that function will scan both string contents, byte by byte, and return a value that indicates if they're equal or not. Look for the detailed usage and examples in the link.

BTW, I think your two macros for shortening printf and scanf aren't a good idea. Refedining those as p and s only buys you less typing, while hindering readability to others (and maybe yourself in a few months). The thing is, any developer knows what printf means (once they know C, of course), but just p will make people go around looking for the actual meaning of that.

Alejandro
  • 7,290
  • 4
  • 34
  • 59