0

My program ends everytime I input a number when I ask the user for the address, I realize this everytime I input a character it keeps going but when I enter both a string and a number it automatically ends

#include <stdio.h>

int main() 
{
    int createAccount;
    char firstName[10];
    char lastName [10];
    char address[20];
    char city[15];
    int zip;
    int existingUser;
    int customerSupport;
    int pendingStatements;
    char userName[10];
    printf("1. Create Account\n");
    printf("2. Login to existing user\n");
    printf("3. Customer support\n");
    printf("4. Check pending statements \n");
    printf("Enter: ");

    scanf("%d, %d, %d, %d", &createAccount, &existingUser, &customerSupport, &pendingStatements);

    if (createAccount == 1)
    {
        printf("Name: ");
        scanf("%s", firstName);

        printf("Last Name: ");
        scanf("%s", lastName);

        printf("Address:  ");
        scanf("%s", address);

        printf("City: ");
        scanf("%s", city);

        printf("Zip: ");
        scanf("%d", &zip);
    }
    else if (existingUser == 2)
    {
        printf("Username: ");
        scanf("%s", userName);
    }

}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Cloudss
  • 25
  • 4
  • `scanf()` was created to read in **f**ormatted data (scan**f**). User input is a looooong way away from formatted. Prefer `fgets()` (possibly followed by `sscanf()`) for user input. – pmg Oct 14 '19 at 16:02

3 Answers3

3

Your first scanf() is trying to read 4 numbers separated by commas. But the user is only entering one menu choice at a time, not 4 different numbers. Because of this, none of the remaining scanf() calls are working.

int choice;
scanf("%d", &choice);
if (choice == 1) {
    ...
} else if (choice == 2) {
    ...
} else if (choice == 3) {
    ...
} else if (choice == 4) {
    ...
} else {
    printf("Invalid choice %d\n", choice);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • hello I did what you did and am still having problem every time I enter the address of the first if statement it always ends everytime i enter a number for example 123 street – Cloudss Oct 14 '19 at 16:20
  • `%s` only processes one word, not a whole line. You should use `fgets()` to read a whole line. – Barmar Oct 14 '19 at 16:23
  • If you put spaces between words it didn't work. thre for use `,` instead of space – Kalana Oct 14 '19 at 16:25
  • fgets doesnt work, and what do you mean by use comma instead of space? – Cloudss Oct 14 '19 at 16:46
  • See https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf for how to mix `fgets()` and `scanf()`. I don't know what he meant about comma. – Barmar Oct 14 '19 at 16:47
1

In scanf If you use more than one variables when you input first value it assign into first given variable.In good coding scanf use for only one variable.

Ex-:

int num1, num2;
printf("Enter numbers = ");
scanf("%d %d", &num1, &num2);

printf("%d\n", num1);
printf("%d\n", num2);

output

Enter numbers = 1
//It shows blank space it means you have to enter second number
1
2

//You can input two numbers by puting spaces among them. Then it will assign given variables
Enter number = 1 2
1
2

you can see first entered value always assign into first variable. There for we don't need multiple variables to do one work. Use only one variable is fine

There for in your case you have to use one variable for check status

#include <stdio.h>

int main() 
{

    int choose = 0;
    char firstName[10];
    char lastName [10];
    char address[20];
    char city[15];
    int zip;
    char userName[10];

    printf("1. Create Account\n");
    printf("2. Login to existing user\n");
    printf("3. Customer support\n");
    printf("4. Check pending statements \n");
    printf("Enter: ");

    scanf("%d", &choose);


    if (choose== 1)
    {
        printf("Name: ");
        scanf("%s", firstName);

        printf("Last Name: ");
        scanf("%s", lastName);

        printf("Address:  ");
        scanf("%s", address);

        printf("City: ");
        scanf("%s", city);

        printf("Zip: ");
        scanf("%d", &zip);
    }
    else if (choose== 2)
    {
        printf("Username: ");
        scanf("%s", userName);

    }
    else if (choose== 3)
    {
        printf("..........");
    }
    else if (choose== 4)
    {
        printf("..........");
    }
    else
    {
        printf("Error number\n");
    }

    return 0;

}
Kalana
  • 5,631
  • 7
  • 30
  • 51
0
#include <stdio.h>

int main() 
{

//int createAccount;
char firstName[10];
char lastName [10];
//int address;
char address[20];
char city[15];
int zip;
char password[10];
int choice;



// int existingUser;
// int customerSupport;
// int pendingStatements;
char userName[10];
printf("1. Create Account\n");
printf("2. Login to existing user\n");
printf("3. Customer support\n");
printf("4. Check pending statements \n");
printf("Enter: ");
scanf("%d", &choice);

if (choice == 1)
{
    printf("Name: ");
    scanf("%s", firstName);

    printf("Last Name: ");
    scanf("%s", lastName);

    printf("Address:  ");
    scanf("%s", address);

    printf("City: ");
    scanf("%s", city);

    printf("Zip: ");
    scanf("%d", &zip);
}
else if (choice == 2)
{
    printf("Username: ");
    scanf("%s", userName);
    printf("Password: ");
    scanf("%s", password);
}


}
Cloudss
  • 25
  • 4
  • I fixed the problem but my new problem is the first if statement when asking the adress when i enter a number and a string it automatically stops it (ex: 123 street) but when I just enter (ex: street) it continues I am trying to figure out now on how I could enter a string and a number – Cloudss Oct 14 '19 at 16:34
  • If you put spaces between words it didn't work. thre for use `,` instead of space – Kalana Oct 14 '19 at 16:42
  • what do you mean put , instead? so i have to do 123, street? – Cloudss Oct 14 '19 at 16:46
  • use `123,street` – Kalana Oct 14 '19 at 18:05
  • ok thanks for the answer I just basically added another placeholder and so users can add numbers onto it – Cloudss Oct 14 '19 at 23:22