The problem is at the age part, the compiler does not give me any errors but when I run it it prints a random number for int age
printf("Enter your name:");
scanf(" %s",&name1);
int age;
printf("\n\nHow old are you?");
scanf(" %d",&age);
char gender;
printf("\n\nEnter your gender[Male/Female]:");
scanf(" %s",&gender);
char confirmation;
printf("Confirmation: Your name is %s , you are %d years old , and you are a %s.\n\nAnswer[Y/N]:",&name1,age,&gender);
Here is your problem.
char gender;
scanf(" %s",&gender);
gender is a char. That is, it only has memory for a 1 byte character. But you are using it as a string. You probably have the same problem for name1 since you are using & for that as well but can't be sure as you don't show that.
Change that to be something like:
char gender[8] // Enough to fit "Female" and terminating NULL
scanf("%7s", gender);
Extra note: scanf is a bit awkward to use to prevent buffer safety. May consider something like fgets with sscanf instead.
There is also dynamic allocation, where you now do not have to specify the amount of storage to use. Using the length modifier %m with the string type modifier s:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *name = NULL
char *gender = NULL;
int age;
printf("Enter your name: ");
scanf("%ms", &name);
printf("\nHow old are you? ");
scanf("%d", &age);
printf("\nEnter gender: ");
scanf(" %ms", &gender);
printf("\n%s %d %s\n", name, age, gender);
free(name); // free the memory
free(gender); //
return 0;
}
In the last couple of lines you will notice a several calls to free. This is because you are left with the responsiblility to free the memory allocated by scanf.
As pointed out by #Matt McNabb if you are on a non-posix compliant system, this will not work. You can use a in place of m, while including #define _GNU_SOURCE on the first line.
Related
Can't figure out why my printf output won't print the int data.age or addr.zip correctly. They're defined as int, so %d should work...but it doesn't. I get gibberish numbers for the output. All other fields work perfectly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct BillingAddress {
char *street[50];
char *city[25];
char *state[3];
int *zip[6];
}address;
address addr;
typedef struct ContactInfo {
char *name[30];
int *age[3];
char *phone[15];
struct BillingAddress PersonalData;
}personaldata;
personaldata data;
int main()
{
printf("Enter your full name: ");
scanf(" %49[^\n]", &data.name);
printf("Enter your age: ");
scanf("%d", &data.age);
printf("Enter your Phone Number (xxx) xxx-xxxx: ");
scanf(" %14[^\n]", &data.phone);
printf("Enter your street address: ");
scanf(" %49[^\n]", &addr.street);
printf("Enter your city: ");
scanf(" %24[^\n]", &addr.city);
printf("Enter your state abbreviation: ");
scanf("%s", &addr.state);
printf("Enter your zip code: ");
scanf("%d", &addr.zip);
printf("Personal Data: \n %s\n %d\n %s\n %s\n %s\n %s\n %d\n", data.name, data.age, data.phone, addr.street, addr.city, addr.state, addr.zip);
}
Let's go step by step. In your BillingAddress struct definition, you intend to store the street, city, and state. It seems logical that you could represent all of those parameters as strings (which are character arrays in C). I believe that is what you tried to do, but the declaration:
char *street[50];
represents give you an array of 50 char * (i.e., char pointers) NOT 50 chars, which is what you actually want. So, the struct definition should instead be:
typedef struct BillingAddress {
char street[50];
char city[25];
char state[3];
int zip;
} address;
You also intend to store the zip. Note how I changed the type of zip to an int array rather than an int array. You could do an int array, but you would have to do more work to properly accept the user's input (simply using scan("%d", &addr.zip) will not work).
Now, let's take a looked at a different way to define your definition for ContactInfo struct:
typedef struct ContactInfo {
char name[30];
int age;
char phone[15];
address addr;
} personaldata;
You can see that I've removed the *'s and changed age to be a single int for the similar reasoning as what I mentioned above. I've also replaced struct BillingAddress with the simpler address. Why? Because you created a typedef linking the keyword address to struct BillingAddress. So, by using address alone, the code looks a bit cleaner and you are putting that typedef to good use :D (Note that there's nothing incorrect about using struct BillingAddress, I just think it looks cleaner to utilize the typedef).
Also, you'll see that I renamed the variable of type address (i.e., of type struct BillingAddress -- remember the typedef means these two are the same) to addr. This makes more sense than naming it PersonalData as you did, because well, it's an address, not arbitrary personal data.
Now moving onto your main() method. You should be aware of some of the pitfalls of using scanf(). Since it reads from the default input stream stdin (i.e., most likely your terminal), any characters not gulped up by scanf() will be left in the input stream. So, any subsequent calls to scanf() will also read in those unwanted characters. See this post for more info. You might not have run into this if you supplied the less than the max number of desired characters, but it is important to take care of this error case. So, after each scanf(), you should clear the input stream, which can be done using a method like:
void clear_input_stream()
{
int c;
while ((c = fgetc(stdin)) != '\n' && c != EOF);
}
You can call this method after each one of your scanf() calls.
You want to make sure that if you are writing into a char array you should not add the & in front of the variable you are writing to -- check this answer for an explanation (your compiler should warn you about that).
The last thing I want to mention is that you could also add a length to the %d specifier to limit reading an integer of a certain length. Your main() could look something like this:
int main()
{
printf("Enter your full name: ");
scanf(" %49[^\n]", data.name);
clear_input_stream();
printf("Enter your age: ");
scanf(" %3d", &data.age);
clear_input_stream();
printf("Enter your Phone Number (xxx) xxx-xxxx: ");
scanf(" %14[^\n]", data.phone);
clear_input_stream();
printf("Enter your street address: ");
scanf(" %49[^\n]", addr.street);
clear_input_stream();
printf("Enter your city: ");
scanf(" %24[^\n]", addr.city);
clear_input_stream();
printf("Enter your state abbreviation: ");
scanf(" %2[^\n]", addr.state);
clear_input_stream();
printf("Enter your zip code: ");
scanf(" %5d", &addr.zip);
clear_input_stream();
printf("Personal Data: \n %s\n %d\n %s\n %s\n %s\n %s\n %d\n", data.name, data.age, data.phone, addr.street, addr.city, addr.state, addr.zip);
return 0;
}
int main()
{
int Age;
char Name;
//Age
printf("Type your age: ");
scanf_s("%d", &Age);
printf("Your age is %d\n", Age);
//Name
printf("Type your Name: ");
scanf_s("%s", &Name);
printf("Your name is %s", Name);
return 0; }
It's the 'Name' section which is throwing out an error. I can't figure out why.
UPDATE: I'm coding in Visual Studio. Therefore, "scanf_s" is essentially required.
The error is "Exception thrown at 0x5B49D4EC (ucrtbased.dll) in Project1.exe: 0xC0000005: Access violation writing location 0x001A0000. occurred"
Your problem is that char Name; can only store a single character. Your code is allowing the user to type in multiple characters which are being stored into Name causing a memory error.
Change char Name; to something like char Name[50] so that you can store up-to 49 characters plus the null byte.
Also you should use scanf_s() properly to avoid the error if the buffer (char array) ends up being too small.
Note, you should always check the return from scanf_s() so you know if the user entered valid data or not.
This code works correctly in Visual Studio:
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
int main()
{
int Age;
char Name[50];
printf("Type your age: ");
if(scanf_s("%d", &Age))
{
printf("Your age is %d\n", Age);
printf("Type your Name: ");
if (scanf_s("%s", Name, (unsigned)_countof(Name)))
{
printf("Your name is %s\n", Name);
}
else
{
printf("Name:: Invalid Input\n");
}
}
else
{
printf("Age:: Invalid Input\n");
}
return 0;
}
The problem is that you defined Name as a char - a single character - but you are trying to use it as a string (multiple characters).
To fix this you must either (a) define Name as an array of characters (which would be a string) - such as char Name[100]; or (b) as a pointer (such as char *Name;) - which would require you to malloc() the string before use and free() it after use.
Strings can be tricky, as they are basically just arrays of chars, but that requires you to either know, or find a way to know, how many characters will be in the string. You can read more about how to do that here, in the documentation for scanf_s, which gives this example:
char c[4];
scanf_s("%4c", &c, (unsigned)_countof(c)); // not null terminated
First off I would just use scanf(), not scanf_s().
Furthermore you need to cast your Name variable as a string, which is an array of characters as I have defined it below. Using just char Name, means you have created a variable with room for just one character.
Hope this helps :)
int main()
{
int Age;
char Name[10];
printf("Type your age: ");
scanf("%d", &Age);
printf("Your age is %d\n", Age);
//Name
printf("Type your Name: ");
scanf("%s", &Name);
printf("Your name is %s", Name);
return 0;
}
Fixed the problem by going to...
Tools->Options->Debugging->Symbols and select checkbox "Microsoft Symbol Servers", Visual Studio will download PDBs automatically.
Thanks for everyone's help :)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char firstname[15];
char lastname[15];
char crush_first[15];
char crush_last[15];
int babies;
printf("What is your first name?\n");
scanf("%s", firstname );
printf("What is your last name?\n");
scanf(" %s", lastname);
/* see i have added space before the character conversion but on exectution
of this file no space is in between the two strings*/
printf("What is your crush's first name?\n");
scanf("%s", crush_first );
printf("What is your crush's last name?\n");
scanf(" %s", crush_last );
printf("How many kids will you have?");
scanf("%d", &babies );
printf("%s%s will have a lovely marriage with %s%s and they will have %d kids",firstname,lastname,crush_first,crush_last,babies);
}
now here i want to do is to add space by default in the string. "__etc" i want the string to also store these values . Though i have added space before %s repeatedly but it is not recognizing.
From scanf doc:
s matches a sequence of non-whitespace characters (a string) [...]
Also if someone enters string longer then your receive buffer, you will overflow the buffer.
Maybe use fgets if you want to read the line up until a newline:
fgets(lastname, sizeof(lastname), stdin);
I am just learning C and making a basic "hello, NAME" program. I have got it working to read the user's input but it is output as numbers and not what they enter?
What am I doing wrong?
#include <stdio.h>
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%d", &name);
printf("Hi there, %d", name);
getchar();
return 0;
}
You use the wrong format specifier %d- you should use %s. Better still use fgets - scanf is not buffer safe.
Go through the documentations it should not be that difficult:
scanf and fgets
Sample code:
#include <stdio.h>
int main(void)
{
char name[20];
printf("Hello. What's your name?\n");
//scanf("%s", &name); - deprecated
fgets(name,20,stdin);
printf("Hi there, %s", name);
return 0;
}
Input:
The Name is Stackoverflow
Output:
Hello. What's your name?
Hi there, The Name is Stackov
#include <stdio.h>
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%s", name);
printf("Hi there, %s", name);
getchar();
return 0;
}
When we take the input as a string from the user, %s is used. And the address is given where the string to be stored.
scanf("%s",name);
printf("%s",name);
hear name give you the base address of array name. The value of name and &name would be equal but there is very much difference between them. name gives the base address of array and if you will calculate name+1 it will give you next address i.e. address of name[1] but if you perform &name+1, it will be next address to the whole array.
change your code to:
int main()
{
char name[20];
printf("Hello. What's your name?\n");
scanf("%s", &name);
printf("Hi there, %s", name);
getchar();
getch(); //To wait until you press a key and then exit the application
return 0;
}
This is because, %d is used for integer datatypes and %s and %c are used for string and character types
My code is as follows
typedef struct
{
char name[15];
char country[10];
}place_t;
int main()
{
int d;
char c;
place_t place;
printf("\nEnter the place name : ");
scanf("%s",place.name);
printf("\nEnter the coutry name : ");
scanf("%s",place.country);
printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
scanf("%c",&c);
printf("You entered %c",c);
return 0;
}
If I run the program, it prompts for place name and country name, but never waits for the character input from user.
I tried
fflush(stdin);
fflush(stdout);
Neither work.
Note : Instead of a character, if I write a similar code to get an integer or a float, it prompts for values and the code works just fine.
int d;
printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
scanf("%d",&d);
Why does this happen? Is there anything wrong in the code?
The problem is that scanf leaves the whitespace following entered non-whitespace characters in the stream buffer, which is what the scanf(%c...) then reads. But wait a second...
In addition to being tricky to get right, such code using scanf is horribly unsafe. You're much better off using fgets and parsing the string later:
char buf[256];
fgets(buf, sizeof buf, stdin);
// .. now parse buf
fgets always gets a full line from the input, including the newline (assuming the buffer is large enough) and you thus avoid the problem you're having with scanf.
You can use string instead of character for scanf.
printf("\nEnter the place name : ");
scanf("%s%*c",place.name);
printf("\nEnter the coutry name : ");
scanf("%s%*c",place.country);
printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
scanf("%c",&c);
printf("You entered %c",c);
Try adding spaces before the % sign in scanf().
I have provided the modified code below.
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[15];
char country[10];
} place_t;
int main()
{
int d;
char c;
place_t place;
printf("\nEnter the place name : ");
scanf(" %s",place.name);
printf("\nEnter the coutry name : ");
scanf(" %s",place.country);
printf("\nEnter the type of the place : Metropolitan/Tourist (M/T)?");
scanf(" %c",&c);
printf("You entered %c",c);
return 0;
}