I want to scan and print two strings one after another in a loop.But I cannot do it.Only one string gets scanned and printed if i use the loop.If i try to print without the loop then the two "gets()" work properly.
#include <stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
printf("Case %d: ",i+1);
//scanf("%[^\n]s",name1);
gets(name1);
/*for(j=0; j<strlen(name1); j++)
{
printf("%c",name1[j]);
}*/
puts(name1);
//scanf("%[^\n]s",name2);
gets(name2);
/*for(j=0; j<strlen(name2); j++)
{
printf("%c",name2[j]);
}*/
puts(name2);
}
}
Here you go. Use fflush(stdin). It will take two inputs and print them one after the another.
#include<stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
printf("Case %d: ",i+1);
fflush(stdin);
gets(name1);
gets(name2);
puts(name1);
puts(name2);
}
return 0;
}
Edit: As suggested in the comment below, using gets() is not advisable if you do not know the number of characters you wish to read.
After taking testcase from the user, next line gets() function will take the '\n' you have to ignore the scenario.
Here's a tricky solution of this problem. Just use '\n' after %d in scanf function. scanf("%d\n",&T);
#include <stdio.h>
int main(void) {
char s1[100],s2[100];
int i,T;
scanf("%d\n",&T);
for(i = 0; i < T; i++){
printf("Case %d: ",i+1);
gets(s1);
puts(s1);
gets(s2);
puts(s2);
}
return 0;
}
You do not terminate your prints.
stdout is buffered.
Print is only performed after a "\n" or explicit flush.
try something around the lines:
#include <stdio.h>
int main()
{
int T,i,j;
char name1[100];
char name2[100];
scanf("%d",&T);
for(i=0; i<T; i++)
{
#ifdef BAD_CODE
printf("Case %d: ",i+1);
gets(name1);
puts(name1);
gets(name2);
puts(name2);
putchar("\n");
#else //better code
fgets(name1, sizeof(name1)-1, stdin);
fgets(name2, sizeof(name2)-1, stdin);
printf("Case %d: '%s' '%s'\n",i+1, name1, name2);
#endif
}
}
Related
#include <stdio.h>
#include <conio.h>
int main() {
char name[20];
int age[20], i, size;
printf("Enter number of students: ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("\nEnter Student # %d name: ", i+1 );
scanf("%s", &name[i]);
printf("\nEnter Student # %d age: ", i+1 );
scanf ("\n%d", &age[i]);
}
printf("---");
printf("\n Student Information");
for (i = 0; i < size; i++)
{
printf("\nStudent # %d :\n", i+1);
printf("Name: %c", name[i]); //how do i print the array here?
printf("\nAge: %d", age[i]);
}
return 0;
}
For some reason, I can't display the whole characters that are inputted, and instead, my program produces nothing. What's causing this and how can I fix it?
starboy_b already answered your question, but I want to highlight security issues in your code:
Keep in mind that using scanf to get input like you do is introducing dangerous vulnerabilities to your program.
Why? :
scanf does not know how much allocated space your variables have and does not prevent the user from entering more characters than your buffer can contain. This lead to buffer overflow and can be used to run attacker code.
Your better off e.g. using readline or fgets to get input from user.
#include <stdio.h>
#include <conio.h>
int main() {
char name[20][100];
int age[20], i, size;
printf("Enter number of students: ");
scanf("%d", &size);
for (i = 0; i < size; i++)
{
printf("\nEnter Student # %d name: ", i+1 );
scanf("%s", &name[i]);
printf("\nEnter Student # %d age: ", i+1 );
scanf ("\n%d", &age[i]);
}
printf("---");
printf("\n Student Information");
for (i = 0; i < size; i++)
{
printf("\nStudent # %d :\n", i+1);
printf("Name: %s", name[i]); //how do i print the array here?
printf("\nAge: %d", age[i]);
}
return 0;
#include <stdio.h>
int main() {
int n, i;
char arr[20];
clrscr();
printf("Enter size of array(<=20)");
scanf("%d", &n);
printf("Enter array");
for (i = 0; i < n; i++) {
scanf("%s", &arr[i]);
}
for (i = 0; i < n; i++) {
printf("%s", arr[i]);
}
getch();
return 0;
}
The program does not prints the array
and instead shows
Program termination message
The image shows the program termiantion message
The problem is with the line
printf("%s", arr[i]);.
If you change this line to
printf("%c", arr[i]);
then it will work because %s is used with character arrays that contain strings
I am just giving the solution for your program termination. Still we can do some modification in your code.
Thanks
i was new with array for C programming. Anybody can help me? I want to print all array that saved from the assignment. Please help!
#include <stdio.h>
int main(){
const char *a[3];
char s[100];
int data, i=0;
back:
printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
if (data==1){
i++;
goto back;}
else{
printf("%s", a[0] , a[1] , a[2]);}
return 0;
}
My suggestion would be never to use goto statement in C , it really mess up the readability of your source code. Also, go through this link, Why goto statement is not generally used.
Apart from this, you code can be simple re-written like this using for-loop statement.
#include <stdio.h>
int main(){
char *a[3];
char s[100];
int data = 1, i;
for(i=0; i<3 && data == 1; i++) {
printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
}
for(i=0; i<3; i++)
printf("%s", a[i]);
return 0;
}
#include <stdio.h>
int main()
{
const char *a[3];
char s[100];
int data, i = 0, j;
back: printf("Please insert name: ");
scanf(" %s", &s);
a[i] = s;
printf("Do you want to add data?: ");
scanf("%d", &data);
i++;
if (data == 1)
{
goto back;
}
else
{
for (j = 0; j < i; j = j + 1)
printf("%s ", a[j]);
}
return 0;
}
I have write simple code using structure method and in that code I am taking string(name) as input using for loop for 3 students. For 1st iteration each line in for loop is working as it should be.... but problem comes on second iteration... in second iteration scanf is skipping input for string (name)...
My code is as below :
#include<stdio.h>
struct student_data
{
char name[5];
int dsd;
int dic;
int total;
};
void main()
{
struct student_data s[3];
int i;
for(i = 0;i<3;i++)
{
printf("Enter Name of Student\n");
scanf("%[^\n]",s[i].name); // Problem occures here in
// second iteration
printf("\nEnter marks of DSD\n");
scanf("%d",&s[i].dsd);
printf("Enter marks of DIC\n");
scanf("%d",&s[i].dic);
s[i].total = s[i].dsd+s[i].dic;
}
printf("%-10s %7s %7s %7s\n ","Name","DSD","DIC","Total");
for(i=0;i<3;i++)
{
printf("%-10s %5d %6d %6d\n",s[i].name,s[i].dsd,s[i].dic,s[i].total);
}
}
The main problem with your code was that you defined the student_data s[2] which is a array of size two, but in the loop, you are looping for (i=0; i<3; i++) which is valid for an array of size 3. This small modifications will work fine:
int main()
{
struct student_data s[2]; // array of size 2
int i;
for(i=0; i<2; i++) // i will have values 0 and 1 which is 2 (same as size of your array)
{
printf("Enter Name of Student\n");
scanf("%s", s[i].name);
printf("\nEnter marks of DSD\n");
scanf("%d", &s[i].dsd);
printf("Enter marks of DIC\n");
scanf("%d", &s[i].dic);
s[i].total = s[i].dsd+s[i].dic;
}
printf("%-10s %7s %7s %7s\n ","Name","DSD","DIC","Total");
for(i=0; i<2; i++) // same mistake was repeated here
{
printf("%-10s %5d %6d %6d\n",s[i].name,s[i].dsd,s[i].dic,s[i].total);
}
return 0;
}
hi my program is to enter a number which gives the length of the string then the string and then finally a letter which should then tell me how many times that letter is in the string. Currently to help me figure out what is wrong with my code i can see that the strcmp is resulting in the same ascii number but negative. eg for the letter a the number is 97 but the strcmp is giving out -97 so the strcmo doesnt show the character as being in the string and results in the incorrect result. Any help would be greatly appreciated. thanks
#include
#include
int main(void)
{
char myChar[100], z, k;
int counter, n, g=0, r, i, l;
counter=0;
scanf("%d",&n);
while (counter<n)
{
counter++;
scanf(" %c",&myChar[counter]);
}
scanf(" %s", &z);
for(i=0;i<n+1;++i)
{
k=myChar[i];
r=strcmp(&z, &k);
l=r;
//printf("\n%c", myChar[i]);
printf("%d\n", l);
if(r==0)
{
g++;
printf("%d\n", g);
}
}
printf("\n\n%d\n", g);
return (0);
}
#include <stdio.h>
#include <string.h>
int main(void){
char myChar[100], str[64];//
int counter, n;
int i, j, k;
printf("number of charactors : ");
scanf("%d",&n);
for (i=0; i<n; ++i){
scanf(" %c", &myChar[i]);
}
printf("input string : ");
scanf("%63s", str);//"%s" : The useless one letter to input as a string. "%c" for &z
for(i=0;i<n;++i){
counter = 0;
for(j=0;str[j]!='\0'; ++j){
k = (str[j] == myChar[i]);//Comparison of each character
counter += k;//if(str[j] == myChar[i]) ++counter;
}
printf("%c is %d\n", myChar[i], counter);
}
return (0);
}