Help needed - C lang

Hello, fellow campers,
I have been a project in C lang.

The project’s focus is to get the info from the end-user.
It is based on a train booking.

The problem is when I get the no. of passengers. if it is 2 then, I ask for some info, after asking, it did not run the next code.

Here is my Code

#include <stdio.h>
#include <stdlib.h>






int main()
{

    char Fname[10];
    char Lname[10];
    int age;
    int no_people;
    int phone;
    char state_des[10];
    char city_des[10];
    char station_des[10];
    char state_origin[10];
    char city_origin[10];
    char station_origin[10];
    char secpass[10];
    char thrpass[10];
    char fourpass[10];
    char fivepass[10];
    int secpass_age;
    int thrpass_age;
    int fourpass_age;
    int fivepass_age;

    printf ("Enter your First Name: ");
    scanf ("%s",Fname);
    printf ("Enter your Last Name: ");
    scanf ("%s", Lname);

    printf ("Enter your Age: ");
    scanf ("%d",&age);
    printf ("Enter your Phone Number: ");
    scanf ("%d",&phone);

    printf ("Enter the Number of Passenger: ");
    scanf ("%d",&no_people);
    if (no_people == 2)
    {
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", secpass_age);

    } else if (no_people == 3)
    {
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", secpass_age);
        printf ("Enter the Name Of the 3th Passenger: ");
        scanf ("%s", thrpass);
        printf ("Enter the Age of the 3th Passenger: ");
        scanf ("%d", thrpass_age);

    } else if (no_people == 4)
    {
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", secpass_age);
        printf ("Enter the Name Of the 3th Passenger: ");
        scanf ("%s", thrpass);
        printf ("Enter the Age of the 3th Passenger: ");
        scanf ("%d", thrpass_age);
        printf ("Enter the Name of the 4th Passenger: ");
        scanf ("%s", fourpass);
        printf ("Enter the age of the 4th Passenger: ");
        scanf ("%d", fourpass_age);
    } else if (no_people == 5);
    {
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", secpass_age);
        printf ("Enter the Name Of the 3th Passenger: ");
        scanf ("%s", thrpass);
        printf ("Enter the Age of the 3th Passenger: ");
        scanf ("%d", thrpass_age);
        printf ("Enter the Name of the 4th Passenger: ");
        scanf ("%s", fourpass);
        printf ("Enter the age of the 4th Passenger: ");
        scanf ("%d", fourpass_age);
        printf ("Enter the Name of the 5th Passenger: ");
        scanf ("%s", fivepass);
        printf ("Enter the age of the 5the Passenger" );
        scanf ("%d", fivepass_age);
    } else {
        return "Please choose the number between 2-5";
    }


    printf ("Enter the State of the Origin: ");
    scanf ("%s", state_origin);
    printf ("Enter the City of the Origin: ");
    scanf ("%s", city_origin);
    printf ("Enter the railway station of the Origin: ");
    scanf ("%s", station_origin);

    printf ("Enter the State you want to go: ");
    scanf ("%s", state_des);
    printf ("Enter the City you want to go: ");
    scanf ("%s", city_des);
    printf ("Enter the Name of the Railway Station: ");
    scanf ("%s", station_des);


}

Hope you understand the given info.
If you need more info, please let me know.

Thanks and Happy Coding!

Is there any error message? What inputs create this bug? What is the value in no_people right after it’s set?

Yes,

I think the if statement.

Will switch solve the problem?

What is the full error message?

The if statement is not an input. An input is what the user provides to the program.

Ah. Syntax error on this line. The ; doesn’t belong and changes the meaning of your code.

By the way, your formatting is a bit nonstandard. Following a consistent style standard might help find stuff like this.

1 Like

It stops after getting passengers details.

Syntax error here too

1 Like

What is the error.

I am new to C.
sorry for asking too many questions.

Well, what’s different from this line?

This one has a pointer to the variable and the other has the value of the variable

1 Like

There a ampersand before the int

Yep.

With C, you can pass the value of a variable to a function or a reference to the variable.

In this case, using the & means you are telling scanf where to store the input.

Do i did to have for all int a ampersand before it?

Yep. You need to pass a reference into scanf.

You are passing a reference in for the character arrays as well, technically.

1 Like

now

#include <stdio.h>
#include <stdlib.h>






int main()
{

    char Fname[10];
    char Lname[10];
    int age;
    int no_people;
    int phone;
    char state_des[10];
    char city_des[10];
    char station_des[10];
    char state_origin[10];
    char city_origin[10];
    char station_origin[10];
    char secpass[10];
    char thrpass[10];
    char fourpass[10];
    char fivepass[10];
    int secpass_age;
    int thrpass_age;
    int fourpass_age;
    int fivepass_age;

    printf ("Enter your First Name: ");
    scanf ("%s",Fname);
    printf ("Enter your Last Name: ");
    scanf ("%s", Lname);

    printf ("Enter your Age: ");
    scanf ("%d",&age);
    printf ("Enter your Phone Number: ");
    scanf ("%d",&phone);

    printf ("Enter the Number of Passenger: ");
    scanf ("%d",&no_people);
    if (no_people == 2)
    {
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", &secpass_age);

    } else if (no_people == 3)
    {
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", &secpass_age);
        printf ("Enter the Name Of the 3th Passenger: ");
        scanf ("%s", thrpass);
        printf ("Enter the Age of the 3th Passenger: ");
        scanf ("%d", &thrpass_age);

    } else if (no_people == 4)
    {
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", &secpass_age);
        printf ("Enter the Name Of the 3th Passenger: ");
        scanf ("%s", thrpass);
        printf ("Enter the Age of the 3th Passenger: ");
        scanf ("%d", &thrpass_age);
        printf ("Enter the Name of the 4th Passenger: ");
        scanf ("%s", fourpass);
        printf ("Enter the age of the 4th Passenger: ");
        scanf ("%d", &fourpass_age);
    } else if (no_people == 5)
        printf ("Enter the Name of the 2th Passenger: ");
        scanf ("%s", secpass);
        printf ("Enter the Age of the 2th Passenger: ");
        scanf ("%d", &secpass_age);
        printf ("Enter the Name Of the 3th Passenger: ");
        scanf ("%s", thrpass);
        printf ("Enter the Age of the 3th Passenger: ");
        scanf ("%d", &thrpass_age);
        printf ("Enter the Name of the 4th Passenger: ");
        scanf ("%s", fourpass);
        printf ("Enter the age of the 4th Passenger: ");
        scanf ("%d", &fourpass_age);
        printf ("Enter the Name of the 5th Passenger: ");
        scanf ("%s", fivepass);
        printf ("Enter the age of the 5the Passenger" );
        scanf ("%d", &fivepass_age);
    } else {
        return "Please choose the number between 2-5";
    }


    printf ("Enter the State of the Origin: ");
    scanf ("%s", state_origin);
    printf ("Enter the City of the Origin: ");
    scanf ("%s", city_origin);
    printf ("Enter the railway station of the Origin: ");
    scanf ("%s", station_origin);

    printf ("Enter the State you want to go: ");
    scanf ("%s", state_des);
    printf ("Enter the City you want to go: ");
    scanf ("%s", city_des);
    printf ("Enter the Name of the Railway Station: ");
    scanf ("%s", station_des);


}

But i can’t build and run in codeblocks.

Any error message?

as i can’t even run the program.

Here is the img

as you see in the top bar.

Try restarting the program? Looks like an error in CodeBlocks maybe

1 Like

I can’t understand the message.

Are you getting these same errors message when you try to compile your code?

prog.c: In function ‘main’:
prog.c:72:12: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
     } else if (no_people == 5)
            ^~
prog.c:74:9: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
         scanf ("%s", secpass);
         ^~~~~
prog.c:18:10: warning: unused variable ‘station_origin’ [-Wunused-variable]
     char station_origin[10];
          ^~~~~~~~~~~~~~
prog.c:17:10: warning: unused variable ‘city_origin’ [-Wunused-variable]
     char city_origin[10];
          ^~~~~~~~~~~
prog.c:16:10: warning: unused variable ‘state_origin’ [-Wunused-variable]
     char state_origin[10];
          ^~~~~~~~~~~~
prog.c:15:10: warning: unused variable ‘station_des’ [-Wunused-variable]
     char station_des[10];
          ^~~~~~~~~~~
prog.c:14:10: warning: unused variable ‘city_des’ [-Wunused-variable]
     char city_des[10];
          ^~~~~~~~
prog.c:13:10: warning: unused variable ‘state_des’ [-Wunused-variable]
     char state_des[10];
          ^~~~~~~~~
prog.c: At top level:
prog.c:89:7: error: expected identifier or ‘(’ before ‘else’
     } else {
       ^~~~
prog.c:94:13: error: expected declaration specifiers or ‘...’ before string constant
     printf ("Enter the State of the Origin: ");
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:95:12: error: expected declaration specifiers or ‘...’ before string constant
     scanf ("%s", state_origin);
            ^~~~
prog.c:95:18: error: unknown type name ‘state_origin’
     scanf ("%s", state_origin);
                  ^~~~~~~~~~~~
prog.c:96:13: error: expected declaration specifiers or ‘...’ before string constant
     printf ("Enter the City of the Origin: ");
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:97:12: error: expected declaration specifiers or ‘...’ before string constant
     scanf ("%s", city_origin);
            ^~~~
prog.c:97:18: error: unknown type name ‘city_origin’
     scanf ("%s", city_origin);
                  ^~~~~~~~~~~
prog.c:98:13: error: expected declaration specifiers or ‘...’ before string constant
     printf ("Enter the railway station of the Origin: ");
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:99:12: error: expected declaration specifiers or ‘...’ before string constant
     scanf ("%s", station_origin);
            ^~~~
prog.c:99:18: error: unknown type name ‘station_origin’
     scanf ("%s", station_origin);
                  ^~~~~~~~~~~~~~
prog.c:101:13: error: expected declaration specifiers or ‘...’ before string constant
     printf ("Enter the State you want to go: ");
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:102:12: error: expected declaration specifiers or ‘...’ before string constant
     scanf ("%s", state_des);
            ^~~~
prog.c:102:18: error: unknown type name ‘state_des’
     scanf ("%s", state_des);
                  ^~~~~~~~~
prog.c:103:13: error: expected declaration specifiers or ‘...’ before string constant
     printf ("Enter the City you want to go: ");
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:104:12: error: expected declaration specifiers or ‘...’ before string constant
     scanf ("%s", city_des);
            ^~~~
prog.c:104:18: error: unknown type name ‘city_des’
     scanf ("%s", city_des);
                  ^~~~~~~~
prog.c:105:13: error: expected declaration specifiers or ‘...’ before string constant
     printf ("Enter the Name of the Railway Station: ");
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:106:12: error: expected declaration specifiers or ‘...’ before string constant
     scanf ("%s", station_des);
            ^~~~
prog.c:106:18: error: unknown type name ‘station_des’
     scanf ("%s", station_des);
                  ^~~~~~~~~~~
prog.c:109:1: error: expected identifier or ‘(’ before ‘}’ token
 }
2 Likes