Ask coding questions

← Back to all posts
2
Error in C
SofhinWong1 (3)

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

//variable for A1
int menu;
char Name[100];
char Student_ID[100];
char Room[100];
char Bill[100];
int Meal, Gym, Week;

int main(){
printf("1 Room Booking\n2 Room Availability\n3 Payment Details\nPlease enter your option:");
scanf("%i",&menu);
if(menu==1){
A1:
printf("\nPlease enter your name:");
scanf(" %c",&Name);
printf("\nPlease enter your Student ID:");
scanf("%c",& Student_ID);
printf("\n\nPlease choose your gender.\n1 Male\n2 Female");
goto A1;
}
}

When I run the program, it gives me warning.
warning: format specifies type 'char ' but the argument has type 'char ()[100]' [-Wformat]
May I know how to fix it?

Commentshotnewtop
1
mwilki7 (111)

%c means "read/print me a single character"
You have a bunch of character arrays with 100 spaces to put a single character.
The compiler doesn't know how to put a single character into an array.

Looks like you want to use string (whole words) so just change
scanf(" %c",&Name);
to
scanf " %s",Name);
and anywhere else you see a '%c' replace it with '%s' for a char array.

Note I got rid of the '&' sign, you don't need them to use scanf with arrays.

1
SofhinWong1 (3)

@mwilki7 oh, it works! Thanks so much. %s can read integer and string in one input right?

1
SofhinWong1 (3)

oh ya, BTW, if I my input is string, and I want to make a for loop, how should I do this?
For example,
printf("A1 for single\nA2 for double\nA3 for triple");
scanf("%s",&choice);
if(choice==A1) and if(choice=="A1")
both of these don't work.

1
mwilki7 (111)

@SofhinWong1
Make sure not to mix up:
int choice
with
scanf("%s",&choice)

%d is for ints int choice; scanf("%d", &choice);
%s is for char arrays char choice[100]; scanf("%s", choice);

something like:

char choice[100];
scanf("%s", choice);
while (strcmp(choice, "done") != 0)
{
    if (strcmp(choice, "A1") == 0)
    {
        do stuff here
    }
    scanf("%s", choice);
}

What the code above does is it keeps reading in input until you write "done"
Fill in what you want A1 to do, copy paste and change it a bit if you want "A2" and "A3"

This function:
strcmp(string1,string2)
compares string1 and string2
if they are the same, it will return 0
otherwise it will be something not 0

1
SofhinWong1 (3)

@mwilki7 sorry, I don't quite understand the code you wrote. I am new to C programming and currently writing a program for my assignment.

1
mwilki7 (111)

I'll try to annotate my code

// my input array
char choice[100];

// get first input
scanf("%s", choice);

// if my input "choice" is the same as the word "done" then I am finished
int finished = strcmp(choice, "done");

// while loop: keep looping until 'finished' is 0
// very similar to a for loop
// if 'finished' is 0 then we will not enter the while loop
while (finished != 0)
{
   
    
    // read a new word
    scanf("%s", choice);

    // check to see if I wrote "done"
    finished = strcmp(choice, "done");
}
1
SofhinWong1 (3)

@mwilki7 so the done is we have 100 input for the choice or?

1
mwilki7 (111)

@SofhinWong1 the 100 means you are only allowed words with 100 characters long or shorter for every input

you can keep inputting forever until you write done

1
mwilki7 (111)

@SofhinWong1 for example if I did
‘char choice[3]
then I wouldn’t even be able to write “done” because “done” is 4 letters long and “choice” only had enough space for 3

1
SofhinWong1 (3)

@mwilki7 I must type done fotr it? Can the computer know when it's done by its own?

1
mwilki7 (111)

@SofhinWong1
the computer will never exit the while loop until the while loop condition is false

the current condition we set is while (finished != 0)
and this means "stay in the while loop if finished is not zero"

the only way 'finished' can be zero is if we type 'done' as input

1
SofhinWong1 (3)

@mwilki7 oh okok. Got it.
BTW, I tried to make a for loop for my code, but it seems like doesn't work!

1
SofhinWong1 (3)

@mwilki7 Is there any way to proceed to the next instead of keep repeating the same thing?
Internet:
printf("\nDO you want to subscribe for Internet?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A1_Internet);
if(A1_Internet==1){
A1_Internet_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&I_week);}
if(A1_Internet==2){
A1_Internet=0;}
else{
goto Internet;}

1
mwilki7 (111)

@SofhinWong1 try adding a space in all your scanf function calls
for instance:
scanf("%i",&I_week);
change to
scanf(" %i",&I_week);
(added a space before '%i')

1
SofhinWong1 (3)

@mwilki7 BTW, I want to ask like how to auto assign a number for the user?
here is my code. I want to auto assign the room no for the user when they book the room. Is it possible? And I use for loop, am I write it correctly? Is this in a loop dy?

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

void Room_Type(){
printf("\n1 Single Room\n2 Double Bedded Room\n3 Triple Bedded Room\n4 Four Beeded Room\n5 Main Menu\nPlease enter your option: ");}

int main(){
//variable for menu1
int menu, GDR,i,pay,Laundry_rate=20;
//variables for A1
char A1_Name=malloc(100);
char
A1_SID=malloc(100);
char *A1_Room=malloc(100);
int choose,I_week, L_week,R_week;
int A1_Internet, A1_Laundry, A1_Gym, A1_Meal,A1_total;
int A1_room_rate=400, A1_Internet_rate=40;

//variables for A3
charA3_Name=malloc(100);
char
A3_SID=malloc(100);
char *A3_Room=malloc(100);
int A3_Internet, A3_Laundry, A3_Gym, A3_Meal, A3_room_rate=150, A3_Internet_rate=15;
int A3_L_week, A3_I_week, A3_R_week,A3_total;

//variables for A4
char A4_Name=malloc(100);
char
A4_SID=malloc(100);
char *A4_Room=malloc(100);
int A4_Internet, A4_Laundry, A4_Gym, A4_Meal, A4_room_rate=100, A4_Internet_rate=10;
int A4_L_week, A4_I_week, A4_R_week,A4_total;

//variable for B4
char B4_Name=malloc(100);
char
B4_SID=malloc(100);
char *B4_Room=malloc(100);
int B4_Internet, B4_Laundry, B4_Gym, B4_Meal, B4_room_rate=100, B4_Internet_rate=10;
int B4_L_week, B4_I_week, B4_R_week,B4_total;

//variable for menu2
int check;

int I_total, room_total, L_total,total;

for(i=0;i<=2;i++){
A1_Room[i]=0;
A3_Room[i]=0;
A4_Room[i]=0;
B4_Room[i]=0;
I_total=0;
L_total=0;
room_total=0;
A1_total=0;
A3_total=0;
A4_total=0;
B4_total=0;}

Main_Menu:
printf("\n1 Room Booking\n2 Room Availability\n3 Payment Details\nPlease enter your option:");
scanf("%i",&menu);

if(menu==1)
{
Details:
printf("\n\n1 Male\n2 Female\nPlease enter your option: ");
scanf("%i",&GDR);
Room_Type();
scanf("%i", & choose);}

//male+A1
if(GDR==1 && choose==1){
A1:
printf("\nHow many weeks of room booking?\n");
scanf("%i",&R_week);

Internet:
printf("\nDo you want to subscribe for Internet?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A1_Internet);
if(A1_Internet==1){
A1_Internet_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&I_week);}
if(A1_Internet==2){
A1_Internet=0;}

//Meal:
printf("\nDo you want to subscribe for Meal?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A1_Meal);
if(A1_Meal==1 ){
printf("Sorry, this service is not available for subscription. Thank you.\n");}

//Laundry:
printf("\nDo you want to subscribe for Laundry?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A1_Laundry);
if(A1_Laundry==1){
Laundry_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&L_week);}
if(A1_Laundry==2){
A1_Laundry=0;}
//Gym
printf("\nDo you want to subscribe for Gym?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A1_Gym);
if(A1_Gym==1 ){
printf("Sorry, this service is not available for subscription. Thank you\n");}

    printf("\nPlease enter your name:");
    scanf("%s", A1_Name);
    printf("Please enter your Student ID:");
    scanf("%s",A1_SID);
    room_total= A1_room_rate*R_week;
    L_total = Laundry_rate*L_week;
    I_total = A1_Internet_rate*I_week;
    total = room_total + I_total+L_total;
  
  Pay:
    printf("\n\nHere is your bill.\nWeekly rate for the room\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the laundry\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the Internet: RM%i\n%i weeks of subscription\t\t: RM%i\nTotal amount payable is RM%i.\n",A1_room_rate, R_week, room_total, Laundry_rate,L_week,L_total,A1_Internet_rate,I_week,I_total,total);
    printf("\nDo you want to make payment now or later by cash?\n1 Now\n2 Cash\nPlease enter your option:");
    scanf("%i",&pay);
    if(pay==1){
      printf("\nPlease deposit your money in the slot provided.\nMaximum cash per deposit is 100 pieces of any combined denominations.\n");
      printf("Thank you for your payment.\n\n");
      goto Main_Menu;}
    if(pay==2){
      printf("Please proceed to counter 1. Payment must be made within 7 days. Thank you.\n\n");
      goto Main_Menu;}
    else{
      goto Pay;}}

//male+A2
if(GDR==1 && choose==2){
A2:
printf("\nSorry. This block is not ready for occupancy.");
goto Details;}
//male+A3
if(GDR==1 && choose==3){
A3:
printf("\nHow many weeks of room booking?\n");
scanf("%i",&A3_R_week);

//Internet:
printf("\nDO you want to subscribe for Internet?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A3_Internet);
if(A3_Internet==1){
A3_Internet_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&A3_I_week);}
if(A3_Internet==2){
A3_Internet=0;}

//Meal:
printf("\nDo you want to subscribe for Meal?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A3_Meal);
if(A3_Meal==1 ){
printf("Sorry, this service is not available for subscription. Thank you.\n");}

//Laundry:
printf("\nDo you want to subscribe for Laundry?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A3_Laundry);
if(A3_Laundry==1){
Laundry_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&A3_L_week);}
if(A3_Laundry==2){
A3_Laundry=0;}
//Gym
printf("\nDo you want to subscribe for Gym?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A3_Gym);
if(A3_Gym==1 ){
printf("Sorry, this service is not available for subscription. Thank you\n");}

  printf("\nPlease enter your name:");
  scanf("%s", A3_Name);
  printf("Please enter your Student ID:");
  scanf("%s",A3_SID);
  room_total= A3_room_rate*A3_R_week;
  L_total = Laundry_rate*A3_L_week;
  I_total = A3_Internet_rate*A3_I_week;
  A3_total = room_total + I_total+L_total;

Pay2:
  printf("\n\nHere is your bill.\nWeekly rate for the room\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the laundry\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the Internet: RM%i\n%i weeks of subscription\t\t: RM%i\nTotal amount payable is RM%i.\n",A3_room_rate, A3_R_week, room_total, Laundry_rate,A3_L_week,L_total,A3_Internet_rate,A3_I_week,I_total,A3_total);
  printf("\nDo you want to make payment now or later by cash?\n1 Now\n2 Cash\nPlease enter your option:");
  scanf("%i",&pay);
  if(pay==1){
    printf("\nPlease deposit your money in the slot provided.\nMaximum cash per deposit is 100 pieces of any combined denominations.\n");
    printf("Thank you for your payment.\n\n");
    goto Main_Menu;}
  if(pay==2){
    printf("Please proceed to counter 1. Payment must be made within 7 days. Thank you.\n\n");
    goto Main_Menu;}
  else{
    goto Pay2;}}

//male+A4
if(GDR==1 && choose==4){
A4:
printf("\nHow many weeks of room booking?\n");
scanf("%i",&A4_R_week);

//Internet:
printf("\nDO you want to subscribe for Internet?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A4_Internet);
if(A4_Internet==1){
A4_Internet_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&A4_I_week);}
if(A4_Internet==2){
A4_Internet=0;}

//Meal:
printf("\nDo you want to subscribe for Meal?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A4_Meal);
if(A4_Meal==1 ){
printf("Sorry, this service is not available for subscription. Thank you.\n");}

//Laundry:
printf("\nDo you want to subscribe for Laundry?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A4_Laundry);
if(A4_Laundry==1){
Laundry_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&A4_L_week);}
if(A4_Laundry==2){
A4_Laundry=0;}
//Gym
printf("\nDo you want to subscribe for Gym?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&A4_Gym);
if(A4_Gym==1 ){
printf("Sorry, this service is not available for subscription. Thank you\n");}

  printf("\nPlease enter your name:");
  scanf("%s", A4_Name);
  printf("Please enter your Student ID:");
  scanf("%s",A4_SID);
  room_total= A4_room_rate*A4_R_week;
  L_total = Laundry_rate*A4_L_week;
  I_total = A4_Internet_rate*A4_I_week;
  A4_total = room_total + I_total+L_total;

Pay3:
  printf("\n\nHere is your bill.\nWeekly rate for the room\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the laundry\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the Internet: RM%i\n%i weeks of subscription\t\t: RM%i\nTotal amount payable is RM%i.\n",A4_room_rate, A4_R_week, room_total, Laundry_rate,A4_L_week,L_total,A4_Internet_rate,A4_I_week,I_total,A4_total);
  printf("\nDo you want to make payment now or later by cash?\n1 Now\n2 Cash\nPlease enter your option:");
  scanf("%i",&pay);
  if(pay==1){
    printf("\nPlease deposit your money in the slot provided.\nMaximum cash per deposit is 100 pieces of any combined denominations.\n");
    printf("Thank you for your payment.\n\n");
    goto Main_Menu;}
  if(pay==2){
    printf("Please proceed to counter 1. Payment must be made within 7 days. Thank you.\n\n");
    goto Main_Menu;}
  else{
    goto Pay3;}
}

//female+A1
if((GDR==2 && choose==1) || (GDR==2 && choose==2) || (GDR==2 && choose==3)){
printf("\nSorry. This block is not ready for occupancy.");
goto Details;}
//female+B4
if(GDR==2 && choose==4){
B4:
printf("\nHow many weeks of room booking?\n");
scanf("%i",&B4_R_week);

//Internet:
printf("\nDO you want to subscribe for Internet?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&B4_Internet);
if(B4_Internet==1){
B4_Internet_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&B4_I_week);}
if(B4_Internet==2){
B4_Internet=0;}

//Meal:
printf("\nDo you want to subscribe for Meal?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&B4_Meal);
if(B4_Meal==1 ){
printf("Sorry, this service is not available for subscription. Thank you.\n");}

//Laundry:
printf("\nDo you want to subscribe for Laundry?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&B4_Laundry);
if(B4_Laundry==1){
Laundry_rate;
printf("How many weeks of subscription?\n");
scanf("%i",&B4_L_week);}
if(B4_Laundry==2){
B4_Laundry=0;}
//Gym
printf("\nDo you want to subscribe for Gym?\n1 Yes\n2 No\nPlease enter your option:");
scanf("%i",&B4_Gym);
if(B4_Gym==1 ){
printf("Sorry, this service is not available for subscription. Thank you\n");}

  printf("\nPlease enter your name:");
  scanf("%s", B4_Name);
  printf("Please enter your Student ID:");
  scanf("%s",B4_SID);
  room_total= B4_room_rate*B4_R_week;
  L_total = Laundry_rate*B4_L_week;
  I_total = B4_Internet_rate*B4_I_week;
  B4_total = room_total + I_total+L_total;

Pay4:
  printf("\n\nHere is your bill.\nWeekly rate for the room\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the laundry\t: RM%i\n%i weeks of subscription\t\t: RM%i\nWeekly rate for the Internet: RM%i\n%i weeks of subscription\t\t: RM%i\nTotal amount payable is RM%i.\n",B4_room_rate, B4_R_week, room_total, Laundry_rate,B4_L_week,L_total,B4_Internet_rate,B4_I_week,I_total,B4_total);
  printf("\nDo you want to make payment now or later by cash?\n1 Now\n2 Cash\nPlease enter your option:");
  scanf("%i",&pay);
  if(pay==1){
    printf("\nPlease deposit your money in the slot provided.\nMaximum cash per deposit is 100 pieces of any combined denominations.\n");
    printf("Thank you for your payment.\n\n");
    goto Main_Menu;}
  if(pay==2){
    printf("Please proceed to counter 1. Payment must be made within 7 days. Thank you.\n\n");
    goto Main_Menu;}
  else{
    goto Pay4;}}

if((GDR==1 && choose==5) || (GDR==2 && choose==5)){
  goto Main_Menu;}

else{
  goto Main_Menu;}

if(menu==2){
printf("\n1 Check by type\n2 Check by gender\n3 Main Menu\nPlease enter your option:");
scanf("%i",&check);
if(check==1){
Room_Type();
scanf("%i",&choose);}
}
}