Why b.r is not visible while printing? [closed] - c

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 6 years ago.
Improve this question
I have this structure after scanning and printing p and q are printed while r is not printed can you please let me know why?
struct book
{
int p;
float q;
char r;
};
int main()
{
struct book b;
scanf("%d%f",&b.p,&b.q);
scanf("%c",&b.r);
printf("%d......%f.....%c",b.p,b.q,b.r);
return 0;
}

Problem :
that's because b.r takes in the \n character entered at the end of the previous scanf() statement
scanf("%d%f",&b.p,&b.q);
Solution :
Avoid it by giving a space before %c in the scanf()
scanf(" %c",&b.r);
Why give a space ?
This would consume if there are any whitespaces (' ' or '\n' or '\0') present in the input stream
Suggestion :
next time when you don't get any output when you print, try printing it's ascii value by casting it to int, that way you'd know what value the variable is taking and see it's corresponding character in ascii table.
printf("%d",(int) b.r);
for example, without making any changes to your code except this to your printf statement :
printf("%d......%f.....%d",b.p,b.q,(int)b.r);
you'd get
input :
2
2
output :
2.....2.....10
why the 10?
because it's the ascii value of \n or the newline character

Related

Looping input request from a char array in C [closed]

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 2 years ago.
Improve this question
When running the program and entering the text, it will ask me again and again to insert the input, until I give an input of one character only.
NOTE: it isn't in any sort of cycle loop, I don't know what the problem is. (I'm using cs50 library for the get char).
char nameless[] = { get_char("Insert the text here: ")};
char nameless[] = {get_char("Insert the text here: ")};
If you look up get_char(), it clearly states
Prompts user for a line of text from standard input and returns the equivalent char; if text does not represent a single char, user is reprompted.
You need to enter only one character. Not a line of characters or 'text' as you call it.
Enter char: a
That's it.
Here's a working code example:
#include <cs50.h>
int main(void)
{
// attempt to read character from stdin
char c = get_char("Enter char: ");
// ensure character was read successfully
if (c == CHAR_MAX)
{
return 1;
}
char next = get_char("You just entered %c. Enter another char: ", c);
if (next == CHAR_MAX)
{
return 1;
}
printf("The last char you entered was %c\n", next);
}
You can look it up at CS50 Library for C

Is this program actually valid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#include<stdio.h>
int main()
{
char word[1000];
scanf("word%s", word);
printf("%s", word);
}
It seems that when I input any string, as long as I type out "word" first, I get proper output.
But is this program actually valid
It compiles and therefore is valid from a syntax perspective. It's also fine in order to check that a prefix is used.
However, there are at least two ways to get undefined behaviour:
scanf might store more than 1000 characters (read 999 and one for the final \0)
scanf might read none if the input does not start with "word"
You should therefore check the result of scanf, initialize word, and also limit the maximum number of characters that scanf reads:
#include<stdio.h>
int main()
{
char word[1000] = {0};
int ret = scanf("word%999s", word);
if ( ret == 1 ) {
printf("%s", word);
}
}
this program is valid ,but you have to be careful about buffer overflow , which means if user input more than 999 chars this will lead to undefined behavior , so I suggest this:
scanf("word%999s", word);
also as you said as long as I type out "word" first ,otherwise char word[1000] will be uninitialized.

Why does ScanF take two inputs from string [closed]

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
The first ScanF takes both inputs when I type a character and an integer is expected.
see image below:
As you can see when I type "55ape" for ScanNum_A it sets ScanNum_B = 0.
Why does this happen?
It happens because
ape is still left in the input stream and your second scanf tries to read it but fails as it was expecting integer but found chars.
You can clear the input buffer after your first scanf as below.
//first scanf
int c;
while ((c = getchar()) != '\n' && c != EOF) { }
//second scanf

Format specifier %n not returning the count of characters [closed]

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 6 years ago.
Improve this question
First, I want to make clear that I'm a beginner and this might be a silly question and that I'm probably doing something wrong.
I want to read characters from a string until a , is found and store them in another string. I also want to know how many characters have been read. This is what I'm doing with sscanf:
sscanf(str, "%[^,]s %n ", newstr, &number);
When I try to print number it prints 0 regardless of my input, even when several characters were stored in newstr.
The problem seems to be in the [^,] sub-specifier as %n works as it should without it.
I want to read characters from a string until a ',' is found and store them in another string. I also want to know how many characters have been read.
The s is not needed. It is not part of the "%[^,]" specifier. Also the trailing " " serves no purpose. Should limit input length too. Do not use newstr unless code knows it was filled.
char str[100];
int number = 0;
// sscanf(str, "%[^,]s %n ", newstr, &number);
sscanf(str, "%99[^,], %n", newstr, &number);
if (number) Success();
else Fail(); // do not use newstr

Missing last character [closed]

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 8 years ago.
Improve this question
I am reversing a String without using inbuilt function . its reversing every character but missing last character
here is the program
#include<stdio.h>
void main()
{
char str[10],rev[10];
int i,j,k;
clrscr();
printf("enter the string \n");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
k=i-1;
for(j=0;j<=i-1;j++)
{
rev[j]=str[k];
k--;
}
rev[j]='\0';
printf("reverse=%s",rev);
getch();
}
I am not getting why the last Char is missing
k = i;
You miscounted it.
Your expression k = i-1; doesn't leave space for the whole reversed string.
The code that computes the length of the string is missing a semicolon:
The compiler interprets it as
for(i=0;str[i]!='\0';i++)
k=i-1;
while the intention has probably been to have
for(i=0;str[i]!='\0';i++)
;
k = i - 1;
Demo on ideone.
Now that the code is "working", you should fix an error that could cause undefined behavior: limit the length of the input to 9 characters in scanf, like this:
scanf("%9s", str);
Without 9, the user could cause undefined behavior by entering more than nine characters, and overflow your ten-byte buffer.

Resources