Multiple inputs in C [duplicate] - c

This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 5 years ago.
Just starting to learn C/C++ in my CS 221 class.
Here's the start of my code:
int main()
{
int id;
char name[100];
double wages;
printf("Enter employee's id: ");
scanf("%4i", id);
printf("Enter employee's full name: ");
scanf("%99[^\n]",name);
printf("Enter gross salary for %s :",name);
return 0;
}
Having trouble wrapping my head around char arrays, stdin and buffers in c. Why does the above code skip the user imputing a name? do I need fflush in there? couldn't really get that to work either.

Not sure why you use
scanf("%4i", id);
But try:
scanf("%d", &id );
This fixes the issue of %4i format, and replaces it with %d for "Decimal integer" format.
This also fixes the issue of , id); because written like this, will not allow id to be stored, and would definitely crash.
If you have issues with numbers you are entering crashing from being too big, maybe check the boundaries of your data type int which will be platform specific.
Also change:
scanf("%99[^\n]",name);
to:
scanf("%s\n",name);
This takes the data entered by the user through scanf and interprets it as a "String of characters" and places it in name properly.

Related

Why scanf scans different number than the one I enter? [duplicate]

This question already has answers here:
"printf" only printing variable addresses
(2 answers)
Closed 3 years ago.
So I'm using C for a few days and I didn't have this problem before but now I have a problem with C scanning different number than the one the user enter. I feel like it prints the location of the number but not the number itself. The number I get every time is 6422076 and if I print another number that I've scan from the user it just show the same number -4, 6422072 so I'm pretty sure It has to do with the location the computer storage the numbers.
I tried to print it with a few other ways and always get the same weird number.
void measures()
{
int height;
printf("\nEnter your height:\n");
scanf("%d",&height);
while(height<140 || height>210){
printf("Invalid input, try again: \n");
scanf("%d",&height);
}
printf("height: %d\n",&height);
}
not getting any errors
Here's your problem:
printf("height: %d\n",&height);
You're not printing the value of height. You're printing its address. Remove the address-of operator:
printf("height: %d\n",height);

Do while is not taking inputs in the order defines it only works for 1 scanf [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 3 years ago.
My program should accept 2 int value as the number and the position at which it should be added. After that it should ask wheather you want to insert more Y/N? But my program dosen't take the input of char instead takes a garbage value and keeps on asking for numbers only.
I have tried using seperate scanf for each input.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n;
char opt;
clrscr();
do{
printf("\nEnter the no. to be added and the position:");
scanf("%d%d",&x,&n);
printf("Do you want to insert more elements Y/N?");
scanf("%c",&opt);
printf("opt=%c x=%d n=%d",opt,x,n);
}while(opt!='N'&&opt!='n');
}
My o/p should be the value of each variables instead I get a garbage value in opt and the loop continues as it isn't 'n'.
The new line character is being read into your variables.
Change this line:
scanf("%c",&opt);
To this:
scanf(" %c",&opt);
The space consumes the new line character. This is only necessary for your " %c" format specifier. The "%d" does not require the space.

I am trying to print out the phone number but i am getting an error [duplicate]

This question already has answers here:
Trying to figure out how to output a phone number in c?
(7 answers)
Closed 4 years ago.
just to let you all know im pretty new at programming and i think I am just missing something small but I dont know what it is. When I compile my code I am getting this error "format %d expects a matching int argument [-Wformat=]". I have tried many things. I have tried making all the function types to char and them all to int. I get similar errors.
char *phoneInput(void)
{
char phone[11];
printf("Input the politicians phone number with no spaces: ");
fgets(phone, 10, stdin);
return 0;
}
char printPhone(char phone[11])
{
printf("- Phone Number: (%d%d%d)%d%d%d-%d%d%d%d", phone[0], phone[1], phone[2],
phone[3], phone[5], phone[6], phone[7], phone[8], phone[9]);
return 0;
}
You are missing one more argument for your printf statement. Add phone[4] to your printf like this.
#include <iostream>
using namespace std;
char *phoneInput(void)
{
char phone[11];
printf("Input the politicians phone number with no spaces: ");
fgets(phone, 10, stdin);
return 0;
}
char printPhone(char phone[11])
{
printf("- Phone Number: (%d%d%d)%d%d%d-%d%d%d%d", phone[0], phone[1], phone[2],
phone[3], phone[4], phone[5], phone[6], phone[7], phone[8], phone[9]);
return 0;
}
But I don't understand is, why all your functions have a return type of char* or char when in fact from both functions, you are returning a 0 only.

C: Segmentation fault after trying to print data on screen [duplicate]

This question already has answers here:
Why does scanf require &?
(7 answers)
When should I use ampersand with scanf()
(3 answers)
Closed 4 years ago.
I'm new to C language and having this problem alot, the code compiles with no problems yet still get this message: Segmentation fault
when trying to execute my code on UNIX terminal.
This is the code:
///MAIN FUNCTION///
int main() {
printf("==========================J.U.S.T=======================$
printf("Enter your option number:\n1- working with an existing f$
char choice[1];
scanf("%c",choice);
if(choice[0] == '1'){
printf("first choice");
}
else if(choice[0] =='2' ){
printf("Enter the following data one by one:");
char BookT[50],AUTHORn[50];long int ISBN;
printf("Book Title:");
scanf("%s",BookT);
// sleep(5);
printf("Author name:");
scanf("%s",AUTHORn);
// sleep(5);
printf("Book number:");
scanf("%d",ISBN);
// sleep(5);
printf("%s\n%s\n%d\n",BookT,AUTHORn,ISBN);
}
else printf("Wrong choice try again!!");
return 0;
}
I'm using Kali linux to compile the code.
As you probably have noticed, the segmentation fault happens at the line
scanf("%d",ISBN);
scanf requires a pointer to save the result in, but you are passing it the value of the variable ISBN. In order to save the result in the ISBN variable you should send it a pointer to the variable instead: scanf("%d", &ISBN);
(arrays already work (more or less) the same way as pointers, so there is no need to use & when reading strings)
You should add an ampersand and proper format specifier to scanf scanf("%ld",&ISBN);
Just change the line scanf("%d", ISBN); by scanf("%d", &ISBN); , scan requires a pointer and you are trying to save the result without pointer, so the program will just segfault. But if you give him the adress of the variable, he can reach it and save the result in it.

how to use scanf to read an integer from the command line [duplicate]

This question already has answers here:
Access violation writing location 0x00000000. reading int from keyboard
(5 answers)
Closed 4 years ago.
suppose I have the following code:
int number;
scanf("%d", number);
printf("%d", number);
I am entering a number, say 10, as the input, but I am not getting it to print anything. If I change my code so that it scans the input as a string it works fine:
char number[2];
scanf("%s", number);
printf("%s", number);
Am I doing something wrong? And if so, what? I am doing all my work at https://www.onlinegdb.com/online_c_compiler if that changes anything
You need to pass the address of number to scanf: scanf("%d", &number);
The reason that it works when you use a string is because a string in c is really just a pointer to a char, and printfknows that, for a string, it needs to dereference the pointer to get to the chars of the string.

Resources