Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to code a simple program in C asking your age and then display the message:
"You are X years old !"
I can't figure out the bugs in the following code:
#define <cini.h>
#define MY STRING "You are"
int main() {
int age;
printf("How old are you?\n");
if(scanf ("%d", age) != 1) {
printf ("Error message\n");
exit(1);
}
printf ("%s years old!\n", MY STRING, age);
return 0;
}
Thanks for help and happy Xmas !
you missed printing age:
printf ("You are %d years old!\n" age);
and scan value need &
if(scanf ("%d", &age) != 1)
you defined pre-processor with space, space are not valid here:
#define MY_STRING "You are"
You missed & before scanf argument.
if(scanf ("%d", &age) != 1)
^
|
Place & before age
Also change your macro MY STRING to MY_STRING. And also you missed a %d specifier in your last printf
Use :
printf ("%s %d years old!\n", MY_STRING, age);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am a new coder and have no idea what I am doing please help!
The code is reading and taking inputs just fine until it reaches the scanf(" %c", &i);then it skips to the Amount print seemingly ignoring my if statements.Is there somthing wrong with my use of scanF?
Here is the code:
#include <stdio.h>
int main(){
printf("BANK ACCOUNT PROGRAM!\n--------------------\n");
char W,F,A,i,D;
int t=1;
double b,d;
while (t=1){
printf("Enter the old balance:\n");
scanf(" %lf", &b);
printf(" %lf", b);
if(b>=0)
{
printf("Enter the transactions now!\n Enter an F for the transaction type when you are finished.\n");
printf("Transaction Type (D=deposit, W=withdrawal, F=finished):\n");
scanf(" %c", &i);
if(i=F){
printf("Your ending balance is");
printf(" %lf", b);
printf("\n Program is ending!");
return 0;
}
if(i=W){
printf("Amount:");
scanf(" %f", &d);
b= b-d;
printf(" %f",b);}
}
if(b<0);
{
printf("The balance must be maintained above zero!\n");
}
}
return 0;
}
Because your comparison is wrong
if(i=F){ // this is assignment, not comparison
should be
if(i=='F'){ // note also it's comparison to character 'F'
In addition to wrong comparison operators, your way of comparing two chars is also wrong. If you want to check whether the input char i equals to 'F' , you should initialize the char variable F with character 'F', otherwise it would be just unintialized variable and comparison with that variable would be wrong.
You should add these lines just after you initialize F and W.
F = 'F';
W = 'W';
By the way, you can use another variable names like,
char var1 = 'F';
char var2 = 'W';
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm new to C and need to get an user input between brackets in order to make my code work. Something like (1, 4, 5) - it will always be only 3 int numbers.
I have already tried scanf("%d %d %d",&x, &y, &a) but it crashes when i use the parenthesis.
#include <stdio.h>
int main() {
int a, b, c;
while (scanf(" (%d ,%d ,%d ) ", &a, &b, &c) == 3) {
printf("%d,%d,%d\n", a, b, c);
}
return 0;
}
Note that the space in the scanf specification permits arbitrary (0..n) blanks, tabs, newlines to surround the delimiters.
ps: as the commenters noted, there is no reason to not include a simple usable program like the above in your question. In addition to making life easier for people who want to help you, it comforts them to know what you have tried.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I wrote a program which test the age of person , if the age is above 18 it further asks the name and gender of the person and depending on the gender it prints if output.
But after I enter the age I am getting Segmentation Fault error.
I am using debian and I am a beginner in C language.
#include <stdio.h>
#include <stdlib.h>
int main(){
char name[20];
int age;
char gender;
printf("How old are you ? \n");
scanf(" %d",age);
if(age >= 18){
printf("What your name ? \n");
scanf(" %s",&name);
printf("What's your gender ? (m/f) \n");
scanf(" %c",&gender);
if(gender == 'm'){
printf("Welcome Mr. %s \n",name);
}else{
printf("Welcome Ms/Mrs %s \n",name);
}
}else{
printf("Nothing to see here! \n");
}
return 0;
}
You missed & in the line:
scanf(" %d",age);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <stdio.h>
void main() {
int age;
char name;
clrscr();
printf("NAME: ");
scanf("%s", &name);
printf("AGE: ");
scanf("%i", &age);
printf("\n\n\t\t Hi, %s ! You are %i years old."name, age);
getch();
}
I'm having trouble with my output im expecting: Hi, Marvin! You are 16 years old. but the output is always like this: Hi, e error Abnormal program termination! You are 15 years old. The format specifier for name is not working but the age is working what should I do?
First thing I doubt that your code compiles, since in printf() you missed ',' :
printf("\n\n\t\t Hi, %s ! You are %i years old.", name,age);
Second, change following:
char name;
clrscr();
printf("NAME: ");
scanf("%s",&name);
to
char name[100];
clrscr();
printf("NAME: ");
scanf("%s",name);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I made a simple little program and it crashes before it gets to the part of the if statement.
#include <stdio.h>
int main()
{
char name[100];
printf("What is your name?");
scanf("%s", name);
printf("Hello, %s\n",name);
printf("You are trapped in a tunnel. Go through the right doors to get out\n");
int choice;
printf("Choose door 1 or door 2");
scanf("%d", choice);
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
return 0;
}
My program runs fine with no errors, it just crashes...
scanf("%d", &choice);
This is how it was meant to be. choice is a standalone int, so you need to pass its address to scanf to get correct results. What you do now is passing a value that is stored in choice to scanf, while it expects an address.
The C library function int scanf(const char *format, ...) reads
formatted input from stdin.
So it should be like this:
int choice;
printf("Choose door 1 or door 2");
scanf("%d", &choice); //& means the address-of
if (choice == 1){
printf("This is the correct door");
}
else if (choice == 2){
printf("This is the wrong door");
}
else{
printf("Press 1 or 2");
}
You miss & in scanf statement that why it doesn't working..
It should be
scanf("%d",&choice);