For some weird reason when I input the values in the marks section, the printed values are all 0, please help, I tried running a smaller version of the code and it worked I don't know why this doesn't.
here is the code:
Stack overflow keeps giving me an error since apparently most of my question is code so pls ignore this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int marksComm;
char gradeComm[1];
int marksEco;
char gradeEco[1];
int marksCompsys;
char gradeCompsys[1];
int marksProg;
char gradeProg[1];
int marksDis;
char gradeDis[1];
int marksLab;
char gradeLab[1];
int marksPhy;
char gradePhy[1];
printf("CCS001\nMarks> ");
scanf("%d", &marksComm);
printf("Grade> ");
scanf("%s", &gradeComm);
printf("\nCCS009\nMarks> ");
scanf("%d", &marksEco);
printf("Grade> ");
scanf("%s", &gradeEco);
printf("\nCSC111\nMarks> ");
scanf("%d", &marksCompsys);
printf("Grade> ");
scanf("%s", &gradeCompsys);
printf("\nCSC112\nMarks> ");
scanf("%d", &marksProg);
printf("Grade> ");
scanf("%s", &gradeProg);
printf("\nCSC113\nMarks> ");
scanf("%d", &marksDis);
printf("Grade> ");
scanf("%s", &gradeDis);
printf("\nCSC126\nMarks> ");
scanf("%d", &marksPhy);
printf("Grade> ");
scanf("%s", &gradePhy);
printf("\nCSC115\nMarks> ");
scanf("%d", &marksLab);
printf("Grade> ");
scanf("%s", &gradeLab);
printf("\nCourse Code Marks Grade\n");
printf("CCS001 %d %s \n", marksComm, gradeComm);
printf("CCS009 %d %s \n", marksEco, gradeEco);
printf("CSC111 %d %s \n", marksCompsys, gradeCompsys);
printf("CSC112 %d %s \n", marksProg, gradeProg);
printf("CSC113 %d %s \n", marksDis, gradeDis);
printf("CSC115 %d %s \n", marksLab, gradeLab);
printf("CSC126 %d %s \n", marksPhy, gradePhy);
}
Your arrays are two small. C strings are NUL-terminated, so you need a char for the letter, and a char for the NUL. That's two char. But you reserve space for one.
Related
I'm practicing C using structs and pointers, I'm asking to the user to input some info using structs and then using pointers to modify it but I'm having problems when I need to get a string with spaces. I switched all the scanfs for fgets and still getting problems with the output.
#include <stdio.h>
#include <stdlib.h>
#define MAXTSTR 25
int main(int argc, char **argv) {
typedef struct birthday {
int day, year;
char month[10];
} BDATE;
struct employee {
char name[MAXTSTR];
int id;
float salary;
BDATE bday;
} emp;
struct employee *ptrEmp = &emp;
printf("Enter employee name:");
fgets(emp.name, MAXTSTR, stdin );
printf("enter id number:");
scanf(" %d", &emp.id);
printf("enter salary");
scanf(" %f", &emp.salary);
printf("enter birhday:");
scanf(" %d", &emp.bday.day);
printf("enter year:");
scanf(" %d", &emp.bday.year);
printf("enter month:");
fgets(emp.bday.month, MAXTSTR, stdin);
printf("\nName: %s \nID: %d \nSalary: %.2f\n", emp.name, emp.id, emp.salary);
printf("%d %s %d", emp.bday.day, emp.bday.month, emp.bday.year);
printf("\n------------------------------------\n");
printf("Enter employee name:");
fgets(ptrEmp->name, MAXTSTR, stdin);
printf("enter id number:");
scanf(" %d", &ptrEmp->id);
printf("enter salary");
scanf(" %f", &ptrEmp->salary);
printf("enter birhday:");
scanf(" %d", &ptrEmp->bday.day);
printf("enter year:");
scanf(" %d", &ptrEmp->bday.year);
printf("enter month:");
scanf(ptrEmp->bday.month, MAXTSTR, stdin);
printf("\nName: %s \nID: %d \nSalary: %.2f\n",
ptrEmp->name, ptrEmp->id, ptrEmp->salary);
printf("%d %s %d", ptrEmp->bday.day, ptrEmp->bday.month,
ptrEmp->bday.year);
return (EXIT_SUCCESS);
}
INPUT and OUTPUT EXAMPLE
Enter employee name:Luis Oliveira
enter id number:01
enter salary1525.25
enter birhday:05
enter year:1991
enter month:
Name: Luis Oliveira
ID: 1
Salary: 1525.25
5
1991
------------------------------------
Enter employee name:Patricia Santos
enter id number:02
enter salary16546.46
enter birhday:05
enter year:1946
enter month:Fev
Name: Patricia Santos
ID: 2
Salary: 16546.46
5
1946
What I'm doing wrong?
Thank you in advance for your help.
Mixing scanf() and fgets() can be very confusing: scanf() leaves the pending newline in the input stream and fgets() reads it and immediately returns because it has reached the end of line.
You can read the month with scanf("%9s", emp.bday.month); and the employee name with scanf(" %14[^\n]", ptrEmp->name);.
Also check for invalid input causing scanf() to return a value different from the expected 1.
Modified program:
#include <stdio.h>
#include <stdlib.h>
#define MAXTSTR 25
typedef struct birthday {
int day, year;
char month[10];
} BDATE;
struct employee {
char name[MAXTSTR];
int id;
float salary;
BDATE bday;
};
int main(int argc, char **argv) {
struct employee emp;
for (int i = 0; i < 2; i++) {
printf("Enter employee name: ");
if (scanf(" %14[^\n]", emp.name) != 1)
return EXIT_FAILURE;
printf("enter id number: ");
if (scanf(" %d", &emp.id) != 1)
return EXIT_FAILURE;
printf("enter salary: ");
if (scanf(" %f", &emp.salary) != 1)
return EXIT_FAILURE;
printf("enter birthday: ");
if (scanf(" %d", &emp.bday.day) != 1)
return EXIT_FAILURE;
printf("enter year: ");
if (scanf(" %d", &emp.bday.year) != 1)
return EXIT_FAILURE;
printf("enter month: ");
if (scanf("%9s", emp.bday.month) != 1)
return EXIT_FAILURE;
printf("\n\nName: %s\nID: %d\nSalary: %.2f\n", emp.name, emp.id, emp.salary);
printf("Birthday: %d %s %d\n", emp.bday.day, emp.bday.month, emp.bday.year);
printf("\n------------------------------------\n");
}
return EXIT_SUCCESS;
}
int main()
{
char i;
char s;
printf("Enter first char : ");
scanf("%s", &i);
printf("Enter second char : ");
scanf("%s", &s);
printf("%c", i);
printf("%c", s);
}
The output turn out only print second scanf value while the first scanf value does not print out.
int main()
{
char i;
char s;
printf("Enter first char : ");
scanf("%c", &i);
printf("Enter second char : ");
scanf(" %c", &s);
printf("%c", i);
printf("%c", s);
}
When change to use %c both scanf value can be print out.
Why does the %s only store the last input while the first get ignored?
%s is to scan string :
scanf(" %s",&string);
Or
scanf("%s[^\n]",string);
%c to scan only one caracter :
scanf(" %c",&caracter);
in your first code ,if you want to read two string and print them :use this code
#include <stdio.h>
int main()
{
char i[150];
char s[150];
printf("Enter first char : ");
scanf("%s[^\n]",i);//scan the first string
printf("Enter second char : ");
scanf("%s[^\n]",s);//scan the second string
printf("%s", i);
printf("\n");
printf("%s", s);
printf("\n");
printf("%c",i[1]);//if you want to print the second caracter in the string i
printf("\n");
printf("%c",s[0]);//if you want to print the first caracter in the string s
}
This question already has answers here:
Why scanf must take the address of operator
(8 answers)
Closed 2 years ago.
Below is my code.
For simplicity I made the code to take the info about 2 friends for now.
But the problem is my for loop in getting user input.
It only works if I remove this part here
printf("Enter Age: ");
scanf("%d", friends[a].age);
here is the full for-loop
for(a=0; a<2; a++) {
printf("Friend no. %d\n", friends[a].fnum+1);
printf("Enter Name: ");
gets(friends[a].name);
printf("Enter Town: ");
scanf("%s", friends[a].address);
printf("Enter Age: ");
scanf(" %d", friends[a].age);
printf("Enter Course: ");
scanf(" %s", friends[a].course);
printf("Favorite foods! \n");
printf("Dish:");
scanf(" %s", friends[a].fav_dish);
printf("Snack:");
scanf(" %s", friends[a].fav_snack);
printf("Drink:");
scanf(" %s", friends[a].fav_drink);
printf("\n");
}
At first, I thought it was the age part was the problem, so I brought it last. But it did not solve it.
I thought it was the scanf too, so I added a space in scanf(" %s", friends[a].course); But nothing work.
I also tried this
printf("Enter Age: ");
scanf(" %d\n ", friends[a].age);
so I tried deleting
printf("Enter Age: ");
scanf("%d", friends[a].age);
and the loop continued to friend 2
What should I do?
Here is my full source code:
#include <string.h>
struct myFriends {
char name[30];
char address[20];
int age;
char course[20];
char fav_dish[20], fav_snack[20], fav_drink[20];
}; struct myFriends friends[2];
int main () {
int a;
puts("Please enter the following info for 2 friends");
for(a=0; a<2; a++) {
printf("Friend no. %d\n", a+1);
printf("Enter Name: ");
scanf("%s", friends[a].name);
printf("Enter Town: ");
scanf("%s", friends[a].address);
printf("Enter Age: ");
scanf(" %d\n ", friends[a].age);
printf("Enter Course: ");
scanf(" %s", friends[a].course);
printf("Favorite foods! \n");
printf("Dish:");
scanf(" %s", friends[a].fav_dish);
printf("Snack:");
scanf(" %s", friends[a].fav_snack);
printf("Drink:");
scanf(" %s", friends[a].fav_drink);
printf("\n");
}
printf("Here are the details");
for (a=0; a<2; a++) {
printf("\n\n Friend no. %d", a+1);
printf("Name:");
puts(friends[a].name);
printf("Town: ");
puts(friends[a].address);
printf("Age: ");
printf("%d", friends[a].age);
printf("Fave FOODS!: ");
puts(friends[a].fav_dish);
puts(friends[a].fav_snack);
puts(friends[a].fav_drink);
}
return 0;
}
In here, I add & mark to pass pointers to scanf, so try this. I hope this works for you.
#include
#include <stdio.h>
#include <string.h>
struct myFriends {
char name[30];
char address[20];
int age;
char course[20];
char fav_dish[20], fav_snack[20], fav_drink[20];
}; struct myFriends friends[2];
int main () {
int a;
puts("Please enter the following info for 2 friends");
for(a=0; a<2; a++) {
char name[30];
char address[20];
int age;
char course[20];
char fav_dish[20], fav_snack[20], fav_drink[20]
printf("Friend no. %d\n", a+1);
printf("Enter Name: ");
scanf("%s", &name);
friends[a].name=name;
printf("Enter Town: ");
scanf("%s", &address);
friends[a].address=address;
printf("Enter Age: ");
scanf(" %d\n ", &age);
friends[a].age=age;
printf("Enter Course: ");
scanf(" %s", &course);
friends[a].course=course;
printf("Favorite foods! \n");
printf("Dish:");
scanf(" %s", &fav_dish);
friends[a].fav_dish=fav_dish;
printf("Snack:");
scanf(" %s", &fav_snack);
friends[a].fav_snack=fav_snack;
printf("Drink:");
scanf(" %s", &fav_drink);
friends[a].fav_drink=fav_drink;
printf("\n");
}
printf("Here are the details");
for (a=0; a<2; a++) {
printf("\n\n Friend no. %d", a+1);
printf("Name:");
puts(friends[a].name);
printf("Town: ");
puts(friends[a].address);
printf("Age: ");
printf("%d", friends[a].age);
printf("Fave FOODS!: ");
puts(friends[a].fav_dish);
puts(friends[a].fav_snack);
puts(friends[a].fav_drink);
}
return 0;
}
scanf statement after if block is not working can somebody help me please
#include<stdio.h>
main()
{
int input,itemno,input2;
int name1,price1,name2,price2,name3,price3;
printf("enter input:\n");
scanf("%d", &input);
if(input==1)
{
printf("enter number of items:\n");
scanf("%d",&itemno);
if(itemno<=3)
{
printf("enter name and age:\n");
scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);
}
else
printf("you can only enter 3 students");
}
printf("press 1 to enter again: \n");
scanf("%d",&input2);
if(input2==1)
{
printf("hey");
}
}
scanf("%d %d\n %d %d\n %d %d\n",&name1,&price1,&name2,&price2,&name3,&price3);
Don't use escape characters in scanf function call.
just delete the last \n in your scanf
the result is :
result
I'm writing a program that basically just takes a bunch of user input and Outputs it in the form of a check. My problem is that I'm entering 2 sentences and while the second one is fine it completly skips the first one! and the user last name is working but the first name just outputs "z" it is the weirdest thing and my teachers philosophy is figure it out yourself. So can anyone possibly help me?? Here is my code...
#include<stdio.h>
#include<string.h>
int main()
{
char date[8];
int checkNum;
char payeeFirst[10];
char payeeLast[10];
double amount;
char memo[50];
char wordAmount[100];
printf("Please enter the date: ");
scanf("%s", &date);
printf("Please enter the check number: ");
scanf("%d", &checkNum);
printf("Please enter the payee First name: ");
scanf("%s", &payeeFirst);
printf("Please enter payee last name: ");
scanf("%s", &payeeLast);
printf("Please enter amount: ");
scanf("%d", &amount);
printf("Please enter amount in words: ");
fgets (wordAmount, sizeof(wordAmount)-1, stdin);
printf("Please enter memo: ");
fgets (memo, sizeof(memo)-1, stdin);
printf(" \n");
printf("Date: %s .\n", date);
printf("Check Number: %d .\n", checkNum);
printf("Payee: [%s] [%s] .\n", payeeFirst, payeeLast);
printf("Amount: %d .\n", amount);
printf(" Check %d \n", checkNum);
printf(" Date: %s \n", date);
printf("Pay to the\n");
printf("Order of %s %s $%d \n", payeeFirst, payeeLast, amount);
printf("%s", wordAmount);
printf(" \n");
printf(" \n");
printf("Memo: %s \n", memo);
return 0;
}
Your scanf calls all leave the '\n' in the stream (the next scanf will ignore it though).
The first fgets reads an empty string (well ... a string containing a single '\n').
Try reading the numbers with fgets too (to a temporary buffer) and sscanf from the buffer to the correct variable. This way all the '\n' will be accounted for.