Getting specific # of characters - c

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();
}

Related

scanf fails to read a string in C [duplicate]

This question already has answers here:
Reading string from input with space character? [duplicate]
(13 answers)
Closed 4 years ago.
I have a simple C program as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100],b[100];
char *ret;
printf("Enter the string\n");
scanf("%s",a);
printf("Enter the substring to be searched\n");
scanf("%s",b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
When I execute the following program, scanf to read the string into b is not waiting for me to enter the substring and the print statement that prints the substring not found is being printed on the console. I tried to give %sand tried in the scanf statement and removed \n from the printf statements and nothing changed the way it executed the program. It would be great if someone solves this simple problem. Thanks in advance.
You can use scanf ("%[^\n]%*c", variable); with this scanf will read the whole line, instead of stopping when a space is reached.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
scanf ("%[^\n]%*c", a);
printf("Enter the substring to be searched\n");
scanf ("%[^\n]%*c", b);
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
Also you can use fgets
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
char b[100];
char *ret;
printf("Enter the string\n");
fgets(a,100,stdin);//100 is the size of the string, you could use sizeof()
printf("Enter the substring to be searched\n");
fgets(b,100,stdin);//100 is the size of the string, you could use sizeof()
ret= strstr(a,b);
if(ret==NULL)
{
printf("Substring not found\n");
}
else
{
printf("Substring found \n");
}
}
try to use fgets instead of scanf, probably the reason is that the spaces are treated as delimiters, and the parts before the space are treated as a and the part right after the space will be treated as b. Therefore the programme did not prompt you for another input.
For your information: Reading string from input with space character?

CodeBlocks exe stops working

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char string;
printf("Hello\n");
printf("What would you like to do\n");
printf("Here are the options\n");
printf("s : How are you\n");
printf("c : What would you like to search\n");
scanf("%s",&string);
if(string == 'h')
printf("iam fine\n");
else if (string == 's')
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
return 0;
}
When I run this after it says what would you like to search and I type run notepad it stops working.
There are two problems with this scanf:
printf("What would you like to search\n");
scanf("%s",&string);
system(string);
string is a single char - the scanf will result in a buffer overrun.
The %s format specifier only reads until the next whitespace.
To fix the problem you should allocate a larger buffer and read an entire line:
char buffer[1024];
printf("What would you like to search\n");
fgets(buffer, sizeof buffer, stdin);
system(buffer);
Problem 1. defining string as char won't work for you. you need an array.
define char string[100] = {0};
Problem 2. scanf("%s",&string); not required, can be used as scanf("%s",string);
problem 3. if(string == 'h'), wrong. array contents cannot be compared using == operator. you've to use strcmp() function.

unable to use scanf twice for scanning a string and then scanning a char

I tried using scanf twice for scanning a string and then scanning a char. It scans string first and does not execute the second scanf. When I use both %s and %c in a single scanf it works perfectly. Can you tell me why this happens?
#include<stdio.h>
int main()
{
char s[100],ch;
scanf("%s",s);
scanf("%c",&ch); //this does not work
printf("%s %c",s,ch);
return 0;
}
another program which works
#include<stdio.h>
int main()
{
char s[100],ch;
scanf("%s %c",s,&ch); //this works!
printf("%s %c",s,ch);
return 0;
}
Please add a space before %c in scanf().
There is a newline character after the string is read so this is being taken by %c
#include<stdio.h>
int main()
{
char s[100],ch;
scanf("%s",s);
scanf(" %c",&ch);
printf("%s %c",s,ch);
return 0;
}

scanf is not waiting for input

I'm trying to find the bug here, but still don't get it.
I've been debugging and googling it and found some close topics, but there are only solutions which I don't need ATM, and I'm curious why this code is not working:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define BUFFER 256
int main()
{
int missionCode;
char *desc = (char*)malloc(sizeof(char)*BUFFER);
do {
printf("Please enter the mission code (or -1 for exit): ");
scanf("%d", &missionCode);
fflush(NULL);
if (missionCode==-1)
return 1;
} while (missionCode>10);
do {
printf("Please enter a string:\n");
scanf("%[^\n]s", desc); //it doesn't stop here!
fflush(NULL);
if (!strcmp("exit",desc))
return 1;
} while (strlen(desc)<20);
printf("your string:\n%s", desc);
return 0;
}
There's something wrong with the scanf\flushall in the second loop, but I don't find out what.
BTW, this is C ofcourse.
scanf("%d", &missionCode);
leaves the newline in the buffer, so
scanf("%[^\n]s", desc);
immediately finds one and stops. You can add a space
scanf(" %[^\n]s", desc);
to the format to skip initial whitespace.

wrapper function in fgets()

I seek for an expert in c programming. Thanks in advance.
Example:
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
char name[100][52],selection[2]="Y";
int x,nname=1;
float sales;
do
{
printf("Enter name: ");
fflush(stdin);
fgets(name[nname],51,stdin); // i need put a wrapper in here
printf("Enter sales: ");
scanf("%f",&sales);
if (sales<1000)
printf("%s\tgood\n",name[nname++]);
else
printf("%s\tvry good\n",name[nname++]);
printf("Enter another name?(Y/N)");
fflush(stdin);
fgets(selection,2,stdin);
*selection=toupper(*selection);
}while(nname<=100 && *selection=='Y');
for(x=1;x<nname;x++)
printf("%s\n",name[x]); // want print the result without(newline) /n
printf("END\n");
system("pause");
}
How do I print the names without being separated by new lines?
I compiled it with GCC 4.4.1 - MinGW and it works fine.
It launched only a warning. This is the result:
warning: return type of 'main' is not 'int'|
||=== Build finished: 0 errors, 1 warnings ===|
Now it works as you expect.
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include <string.h> // strlen()
void main() {
char name[100][52],selection[2]="Y";
int x,nname=1;
float sales;
do {
printf("Enter name: ");
fflush(stdin);
fgets(name[nname],51,stdin); // i need put a wrapper in here
for (x=0; x<strlen(name[nname]); x++){ // this will discarge the \n
if (name[nname][x] == '\n')
name[nname][x] = '\0';
}
printf("Enter sales: ");
scanf("%f",&sales);
if (sales<1000)
printf("%s\tgood\n",name[nname++]);
else
printf("%s\tvry good\n",name[nname++]);
printf("Enter another name?(Y/N)");
fflush(stdin);
fgets(selection,2,stdin);
*selection=toupper(*selection);
} while(nname<=100 && *selection=='Y');
for(x=1; x<nname; x++)
printf("%s ",name[x]); // want print the result without(newline) /n
printf("\nEND\n"); // inserted \n before END
system("pause");
}
Just use
printf("%s ", name[x]);
instead of
printf("%s\n", name[x]);
The \n character is creating the new lines.
Edit
fgets apparently reads newlines into the buffer - you can strip the newline with
name[nname][strlen(name[nname])-2] = '\0';

Resources