unable to give input everytime for char using do while loop - c

I am not able to give input every time. It is skipping in between and I can only give input at alternate times I have created a do while loop and trying to take input as char.
#include <stdio.h>
int main()
{
char gender;
do{
printf("Enter gender\n");
scanf("%c",&gender);
if(gender=='m')
printf("Male\n");
else
printf("Other\n");
}while(1);
}

The scanf() read the new line too as a character and assign it to next char value.
Thats why you can able to read alternatively.
So read a dummy character next to it solve this.
Try this and let me know
#include <stdio.h>
int main()
{
char gender,temp;
do{
printf("Enter gender\n");
scanf("%c%c",&gender,&temp);
if(gender=='m')
printf("Male\n");
else
printf("Other\n");
}while(1);
}

Related

C Program Crashes in Visual Studio

I am getting this error whenever I run my code in visual Studio:
#include <stdio.h>
#include <ctype.h>
int main() {
char username[10];
printf("Enter Username: ");
scanf_s("%[^\n]", &username);
while (isupper(username)) {
if (username == '-') {
printf("Username cannot contain UpperCase Letters");
}
}
}
Error Image
I don't think you can pass whole array to isupper. Also if you don't want to return anything instead of int main() use void main() or just return 0 in the end or when you want to end after your program executed successfully. As for using scan_s or scanf or getline or whatever I won't say anything because its a different matter and your syntax of scanf_s is certainly wrong.
Also following code will not check for any buffer overflow (not a good practice, you will see even though we gave size 20 char array, this code will work even for larger input which is certainly not a good thing). So you can either limit the size of input or better to read an entire line via fgets() (or getline() if available) and parse the string yourself.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
char username[20];
printf("Enter Username: ");
// scanf("%[^\n]", username); <--- Instead of this
scanf_s("%20c", username, 20); // <----Try Using this
int i=0;
while (i<strlen(username)) {
if (isupper(username[i])) {
printf("Username cannot contain UpperCase Letters\n");
return 0;
}
i++;
}
return 0;
}
My first guess would be that your while is an endless loop, try to do it like this:
int i;
for(i=0; i<strlen(username);i++){
if(isupper(username[i])){
printf("Username cannot contain UpperCase Letters");
}
}

Loop gets terminated without any error

well I have been trying to write this program which accepts any number of integers until the condition is true using do while loop but the loop gets terminated even if the condition is met.I am not able to understand the flaw in my code... I would be thankful for any help
#include<stdio.h>
#include<conio.h>
int main()
{
int max,min,test;
char next;
printf("enter an integer:\n");
scanf("%d",&max);
min=max;
do
{
printf("enter next integer:\n");
scanf("%d",&test);
if(test>max)
max=test;
else if(test<min)
min=test;
printf("want to enter next number y/n\n");
scanf("%c",&next);
}while(next=='y');
printf("Maximum=%d\nMinimum=%d\nRange=%d\n",max,min,max-min);
getch();
}
Try this ...
#include<stdio.h>
#include<conio.h>
int main()
{
int max,min,test;
char next;
printf("enter an integer:\n");
scanf("%d",&max);
min=max;
do
{
printf("enter next integer:\n");
scanf("%d",&test);
getchar(); // added this
if(test>max)
max=test;
else if(test<min)
min=test;
printf("want to enter next number y/n\n");
scanf("%c",&next);
}while(next=='y');
printf("Maximum=%d\nMinimum=%d\nRange=%d\n",max,min,max-min);
getch();
}
After taking input test when you are pressing Enter your code takes Enter as a new line character. To avoid this, take that new line character through getchar().
When reading input through scanf(), It will not consume return key so '\n' will remain there in stdin waiting to read for next char input. That is why your program is coming out of loop. So to avoid this problem use getchar() after scanf("%d",&test);.
in else if (condition) you have written test<min, where variable min does not have any value. you should write
else if(test<max)
(
min = test
)

Getting specific # of characters

I want to get only 2 characters in my program. I tried fgets but I can get it to work. How do you suggest that I implement this? Or is there any alternative?
char code[2];
printf("Enter code: \n");
scanf("%s", code);`
I want to limit the number of characters that can be entered to two.
To read two characters and ignore white space you can do:
#include <stdio.h>
int main() {
char code[2];
printf("Enter code:\n");
if (scanf(" %c %c", &code[0], &code[1]) == 2) {
printf("successfully read '%c' and '%c'\n", code[0], code[1]);
}
return 0;
}
to not ignore white space use "%c%c" as the format.
Try this code
#include<stdio.h>
#include<conio.h>
void main()
{
char code;
clrscr();
printf("enter the code\n");
scanf("%2s",code);
printf("%s",code);
getch();
}

Input problems scanf()/getchar()

I am making a text based game, and i am having a big problem with input. Here is a small example of my problem code.
#include <stdio.h>
#include <stdlib.h>
char c;
int main(int argc, char *argv[]){
system("clear");
while(1){
printf("\nInput a character.\n");
c = getchar();
printf("\nYour input: %c\n", c);
sleep(1);
system("clear");
}
return 0;
}
So, if you compile/run this, and type in 'abc', it will just take each one, and send it through the loop. What I need it to do is only take the very first character that someone types in, no matter how many they do type in.
And, PS: I have tried it this way, and it does the same thing:
#include <stdio.h>
#include <stdlib.h>
char c[2];
int main(int argc, char *argv[]){
system("clear");
while(1){
printf("\nInput a character.\n");
scanf("%1s", c);
printf("\nYour input: %c\n", c[0]);
sleep(1);
system("clear");
}
return 0;
}
EDIT: It also adds a space to what ever you type in, I assume it is a \0, but im not sure. Thanks!
When you use scanf, enter a string and hit the ENTER key, a string and a character are placed in the input buffer, they are namely: the entered string and the newline character. The string or character by character gets consumed by the scanf but the newline remains in the input buffer, unless you consume that too.
getchar(), on the other hand will not wait for ENTER key, it would read character by character, then your logic.
I think you can add 1 more line to read all the characters that come after the first one until there is a newline character (i.e. the user presses Enter):
while (getchar() != '\n');
Adding to your example, it would be like this:
#include <stdio.h>
#include <stdlib.h>
char c;
int main(int argc, char *argv[]){
system("clear");
while(1){
printf("\nInput a character.\n");
c = getchar();
printf("\nYour input: %c\n", c);
sleep(1);
system("clear");
while (getchar() != '\n');
}
return 0;
}
Use getch() which does not wait for a newline.
What i think you look for is something like this code, to save very first character, you can also check if c == '\n' to continue your operation, but i dont know what you want after saving very first character:
int i,c;
char save;
for ( i = 0;(c=getchar())!= EOF ; i++)
{
if ( i == 0)
save = c;
}
You can use fgets(), and extract its first character, like...
char ch[2], c;
fgets(ch, 2, stdin);
c = ch[0];

Program bypass first fgets

The problem is when it asks the user to enter the name of the band.The program bypass the first fgets, but executes the printf and not the fgets. I tried to handle this problem with if but again the same problem. Bypass the first fgets. I provide you an image also.
#include <stdio.h>
#include <stdlib.h>
struct cd
{
char band[100];
};
struct cd *music;
int main()
{
int n,i;
printf("Give how many albums you want: ");
scanf("%d",&n);
struct cd *music = malloc(sizeof(struct cd)*n);
for(i=0;i<n;i++)
{
printf("\nEnter Band: ");
fgets(music->band,sizeof(music->band),stdin);
music++;
}
for(i=n-1;i>=0;i--)
music--;
for(i=0;i<n;i++)
{
printf("\nBand: %s",music->band);
music++;
}
printf("\n\n");
return 0;
}
Scanf() treats '\n' as a character and since it is not in the format string, it leaves it there.
try this
char newline;
int n,i;
printf("Give how many albums you want: ");
scanf("%d%c",&n,&newline);
This will remove the '\n' from the stdin.
when you read with scanf() it reads everything leaving the following '\n' from the end
And Now when you try to read with fgets()it reads the'\n' character left by scanf
To solve this you can use fgetc(stdin); after your scanf so that it gets consume.

Resources