C Program error with char - c

When I execute the program it asks me for my name if I put more than one letter it has an error and it shuts down and in the second one anyting I put it fails and closes instantly
#include <stdio.h>
int main () {
char firstname[20];
char lastname[20];
char response[20];
printf ("Type your first name:\n");
scanf ("'c'",&firstname);
printf ("\n");
printf ("Type your last name:\n");
scanf ("'c'",&lastname);
printf ("\n");
printf ("Hi %s %s do you want to stop giving me information?\nSay Y or N");
scanf ("%s",&response);
if (response == 'Y' || response == 'y'); {
system ("pause<NULL");
}
printf("Thank you for using my program. Good Bye!\n\n");
system ("pause<NULL");
}

There are many issues:
You probably want this (untested code)
#include <stdio.h>
int main () {
char firstname[20];
char lastname[20];
char response[20];
printf ("Type your first name:\n");
scanf ("%s", firstname);
printf ("\n");
printf ("Type your last name:\n");
scanf ("%s", lastname);
printf ("\n");
printf ("Hi %s %s do you want to stop giving me information?\nSay Y or N", firstname, lastname);
scanf ("%s",response);
if (response[0] == 'Y' || response[0] == 'y') {
system ("pause<NULL");
}
printf("Thank you for using my program. Good Bye!\n\n");
system ("pause<NULL");
return 0;
}
There is no 'c' format specifier, it's %s
scanf ("%s", &firstname) is wrong, firstname is already the address of the buffer
response == 'y'is wrong, response is the address of the buffer, you just need the first char of the buffer, that is responde[0]
if (response[0] == 'Y' || response[0] == 'y'); {, there was a stray ; before the {

Your problem is that you are asking for a char input, you want a string.
tutorialspoint has a simlpe and claean example.
https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm

Related

My last prompt answers itself. What do I do?

I'm new to C, and I was trying to create a password system with 2 questions. 1. "What is your password?" I prompted the user to answer and recorded the argument. Any input was assigned to the variable. 2."Are you sure? Do you want this password? Enter Yes or No." It would decide whether to use the answer or discard it. Everything was working smoothly until this part. I couldn't type Yes or No. It automatically assumed that I had typed yes and saved it. Can somebody please give me some advice?
#include <stdio.h>
int
main ()
{
char password;
printf ("Hello User.");
printf ("Please type in your password:");
scanf ("%d", & password);
char answer;
printf ("\nAre you sure? Do you want this password? Enter Yes or No: \n");
scanf ("%c", answer);
printf ("\nAnswer is %c");
if (answer == 'Yes')
printf ("Confirmed.");
else (answer == 'No');
printf ("OK. Thank you.");
password = 0;
return 0;
}
As noted in the comments, you likely didn't intend to store a password in a single character. Nor did you likely intend to scanf the password as an integer using, which is what you're saying with %d.
You may want to use getline instead to read an entire line, and store it in password if you declare it as char array.
Let's say you gave it room for 100 chars.
char password[100];
You might read your password in with:
getline(password, 100, stdin);
You can do the same for the second question, but should always use strcmp to compare strings in C. When the two strings are equal, the return value is 0. Be careful as this is opposite of the way that "true" is usually represented by integers in C.
if (strcmp(response, "yes") == 0) {
...
}
Hopefully these tips will point you in the right direction.
Research and study the functions and syntax.
#include <stdio.h>
#include <string.h>
//function: Clears password
void clearPassword(char p[])
{
for(int i = strlen( p ); i>=0; i--)
{
p[i]='\0';
}
}
int main ()
{
char password[101]; //C version of a String
char line[100]; //C version of a String
char answer;
printf ("Hello User.");
printf ("Please type in your password: ");
fgets(line, sizeof(line), stdin); // read input
sscanf(line, "%s", &password); // store password
printf ("\nAre you sure? Do you want this password? Enter Y or N: \n");
fgets(line, sizeof(line), stdin); //read input
sscanf(line, "%c", &answer); // store answer
printf ("\nAnswer is %c \n", answer); // print answer
if (answer == 'Y') //
printf ("Confirmed.");
else
{
clearPassword(password);
printf ("OK. Thank you.");
}
return 0;
}

Printf function does not works with char

I got a problem, because the printf fuction does not show any result when I try to debug the code. What I am doing wrong? Can someone tell me? I tried to put the buffor, to make a space before the %s in scanf, but it does not work.
struct worker {
char name[50];
};
struct worker tab[1000];
int worker_count=0;
int c = getchar();
if (c == 'D') {
++worker_count;
printf("Put the name:");
fflush(stdin);
scanf_s(" %s", &tab[worker_count].name, 1);
printf("Imie: %s\n",tab[worker_count].name);
}
system("pause");

Not able to know the reason of segmentation fault

I want make a tictactoe game program. It's unfinished yet but probably there are some problems that I can't figure out.
#include <stdio.h>
#include <string.h>
play(){
char input[3][3],player1[100],player2[100];
int i,j,k,times;
for (j=0;j<3;j++){
for (k=0;k<3;k++){
input[j][k]='_';
}
}
printf ("How many times do you want to play?\n");
scanf ("%d",&times);
printf ("Enter the name of first player : \n");
scanf ("%s",player1);
printf ("Enter the name of second player : \n");
scanf ("%s",player2);
printf ("Who will enter first letter?" "\n\n1. %s" "\n1. %s" "\n\nEnter 1 or 2 : \n",player1,player2);
scanf ("%d",&i);
for (j=0;j<times;j++){
for (k=0;k<9;k++){
if (i==1){
if (k==0 || k%2==0){
printf("This is %s's chance.",player1);
}
if (k==1 || k%2==1){
printf("This is %s's chance.",player2);
}
}
if (i==2){
if (k==0 || k%2==0){
printf("This is %s's chance.",player2);
}
if (k==1 || k%2==1){
printf("This is %s's chance.",player1);
}
}
printf ("%s %s %s\n\n%s %s %s\n\n%s %s %s\n\n",input[0][0],input[0][1],input[0][2],input[1][0],input[1][1],input[1][2],input[2][0],input[2][1],input[2][2]);
}
}
}
int main(){
int i;
printf("Welcome to TicTacToe made by Saurabh.\n\n1. Play\n2. Help\n\nEnter 1 or 2 : ");
scanf("%d",&i);
switch (i){
case 1:
play();
break;
case 2:
break;
default:
printf ("Invalid response from user.");
}
}
After running scanf("%d",&i) in the play function it says segmention fault but I can't figure out why that's happening. Thanks in advance.
With some prints I have seen there are some problems in this line:
printf ("%s %s %s\n\n%s %s %s\n\n%s %s %s\n\n",input[0][0],input[0][1],input[0][2],input[1][0],input[1][1],input[1][2],input[2][0],input[2][1],input[2][2]);
You defined char input[3][3] so input[0][0] is a single char (and the others with different indexes too) just like if I define char my_str[10];, my_str[0] is a single char.
I don't know if this is what you want but changing to:
printf ("%c %c %c\n\n%c %c %c\n\n%c %c %c\n\n",input[0][0],input[0][1],input[0][2],input[1][0],input[1][1],input[1][2],input[2][0],input[2][1],input[2][2]);
should not generate the error.

Calling function in if statement or switch not working properly

When I compile and run this the output is:
press n to continue
n
Enter the filename: [ �h�� ]
But, if I call the new(); directly it run perfectly. But when I call new(); in if statement or switch statement, it shows the above output.
I tried scanf, fgets and gets in the new() fucntion but still not working.
#include<stdio.h>
#include<stdlib.h>
int menu();
int new();
int main(){
menu();
return 0;
}
int menu(){
printf("press n to continue\n");
//char c = getc(stdin);
char c = getchar();
if(c=='n'){
new();
}
else if(c==27){
return 0;
}
}
int new(){
char filename[50];
printf("Enter the filename: ");
//fgets(filename, 50, stdin);
scanf("%[^\n]s", filename);
printf("[ %s ]\n\n", filename);
return 0;
}
getchar() will read one character from stdin and leave the \n. So when you call scanf - it stops immediately and you got nothing. To skip whitespaces and start reading from non-space character add space before format.
scanf(" %49[^\n]", filename);
Do not mix %[] and %s
Always specify max number of chars to read (leaving one additional char for nul-terminator)
And compile with highest warning level - so you do not leave menu function without return.
Oh. and check the return value of scanf
if(scanf(" %49[^\n]", filename) == 1)
printf("[ %s ]", filename);

File Handling Error in C

I am learning file handling in C.I have this code but it is not accepting string as an input to write it to a file.Any help will be appreciated.
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main(void)
{
FILE * fp1;
fp1 = fopen("abc.txt","a+");
if(fp1==NULL)
{printf("An error occurred");
}
printf("Delete file?\n");
int a,c;
char name [20];
int flag=1;
int ch=1;
while(flag!=0)
{
printf("Enter id input \n");
scanf("%d",&a);
fprintf(fp1,"\n%d\t",a);
printf("Enter Name");
gets(name);
fputs(name, fp1);
printf("Enter No \n");
scanf("%d",&c);
fprintf(fp1,"\t%d\t",c);
printf("Write more then press 0 else 1");
scanf("%d",&ch);
if(ch==1)
{
flag=0;
}
}
fclose(fp1);
}
On running this code the code does not take an input after Enter Name and directly skips to Enter No.I want the output to be in a tabular form.
Use a getchar() after entering id because the \n of 1st scanf stays in buffer.
printf("Enter id input \n");
scanf("%d",&a);
getchar();
When you enter a number for scanf("%d",&a);, you type in a number and press the Enter key. The scanf consumes the number and leaves the newline character ('\n') in the standard input stream (stdin). When the execution of the program reaches gets(name);, gets sees the newline character and consumes it, storing it in name.
Firstly, never use gets as it is dangerous as it doesn't prevent buffer overflows. Use fgets instead:
fgets(name, sizeof(name), stdin);
Secondly, you have to get rid of the newline character. You can do this by flushing the stdin. Or you can simply scan and discard the newline character just after reading the number from scanf by changing
scanf("%d",&a);
to
scanf("%d%*c",&a);
%*c scans and discards a character.
gets() is deprecated, don't use it. you can still use scanf()...
as for the tabulation...think it through.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE* fp1;
fp1 = fopen("abc.txt", "a+");
if (fp1 == NULL) {
printf("An error occurred");
}
int a, c;
char name [20];
int flag = 1;
int ch = 1;
while (flag != 0) {
printf("Enter id input:\n");
scanf("%d", &a);
fprintf(fp1, "%d\t", a);
printf("Enter Name:\n");
scanf("%s", name);
fprintf(fp1, "%s\t", name);
printf("Enter No:\n");
scanf("%d", &c);
fprintf(fp1, "%d\n", c);
printf("Again (0) or Exit(1) ?:\n");
scanf("%d", &ch);
if (ch == 1) {
flag = 0;
}
}
fclose(fp1);
return 0;
}

Resources