r/code • u/Tall-Lawfulness-6632 • Oct 04 '23
code problem
guys please help what is wrong with my code???the summary doesn't runT^T
#include <stdio.h>
int main()
{
printf("\\tSTUDENT'S REGISTRATION FORM");
char firstname;
printf("\\n\\nEnter First Name: ");
scanf("%s", &firstname);
char lastname;
printf("\\nEnter Last Name: ");
scanf("%s", &lastname);
char gender;
printf("\\nEnter Gender: ");
scanf("%s", &gender);
char course;
printf("\\nEnter Course: ");
scanf("%s", &course);
char month;
printf("\\nEnter Birth month: ");
scanf("%s", &month);
char day;
printf("\\nEnter Birth day: ");
scanf("%s", &day);
char year;
printf("\\nEnter Birth year: ");
scanf("%s", &year);
char age;
printf("\\nEnter Age: ");
scanf("%s", &age);
char school;
printf("\\nEnter School: ");
scanf("%s", &school);
printf("\\n\\n\\nSUMMARY");
printf("\\n\\nName: %s \\t %s", firstname, lastname);
printf("\\nGender: %s", &gender);
printf("\\nBirthday: %s\\t%s, %s", month, day, year);
printf("\\n\\nHello %s\\t%s, you are enrolled as a %s student, welcome to %s", firstname, lastname, course, school);
}
0
Upvotes
View all comments
Show parent comment
1
u/Alone-Veterinarian76 Oct 05 '23 edited Oct 05 '23
Ok and this is c ?
Edit: So I'm no expert obviously as the corrections I had made in the other comment were way off base after some study. I think this link might help.
Edit: Try initializing the variables as an array of char.Ex: char name[20];
scanf("%s", name);
//using this as a model I was able to get it to work.
**spoiler**
// Online C compiler to run C program online
#include <stdio.h>
int main()
{
// Initiate all variables at the same time as char, (this is probably not best practice as month, day, year, and age might be better suited to other types)
char name[20], surname[20], gender[20], course[20], month[20], day[20], year[20], age[20], school[20];
printf("Hello, enter your name: ");
// Assign first variable name %s being your format specifier and &name the address of the name variable.
scanf("%s" , name);
printf("Ok %s please enter your last name: " , name);
scanf("%s" , surname);
printf("Enter your gender: ");
scanf("%s" , gender);
printf("Enter your course: ");
scanf("%s" , course);
printf("Enter the month you were born: ");
scanf("%s" , month);
printf("Enter the day you were born: ");
scanf("%s" , day);
printf("Enter the year you were born: ");
scanf("%s" , year);
printf("Enter your age: ");
scanf("%s" , age);
printf("Enter the school you attend: ");
scanf("%s" , school);
printf("Welcome : %s %s \n Gender: %s\n Course: %s\n Birthdater %s, %s, %s/n Age: %s/n Currently attending: %s" , name, surname, gender, course, month, day, year, age, school);
return 0;
}
https://www.programiz.com/c-programming/c-strings