C Array Looping Error - c

I'm supposed to write a simple C program to read in an integer and loop n times to work on the string, but the first loop automatically passes an empty string if I use the scanf integer, but if I use a constant number the loop works right. Somebody please explain to me what's going on.
#include <stdio.h>
#define MAX 80
int main(){
char sentence[MAX];
int n, i;
scanf("%d", &n);
for(i=0; i<3; i++){//it loops with empty string automatically if I replace 3 with n
gets(sentence);
printf("%s\n", sentence);
}
}

Try this code
#include<stdio.h>
#include<conio.h>
#define MAX 80
void main()
{
char sentence[MAX];
int n,i;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf(" %99[^\n]",sentence);
printf("\n%s\n",sentence);
getch();
}

Related

Why does my program exit after taking an input?

Consider:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf("%[^\n]s", &name);
}
}
Whenever I am entering the value of n, it just prints (Enter n values) and exits the program. The for loop never runs. It ran successfully for the first time, but after that it just exits the program.
There were some answers that said it will not print anything. I don’t want it to print just to take input n times. It is not doing that.
My aim is to take n as input and then take strings of names (like harry, robin, etc.) n number of times as input.
Your code is a little incomplete. And there are a few errors here: scanf ("%[^\n]s", &name)
Do this and everything will be fine:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf(" %d", &n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf(" %99[^\n]", name);
printf("%s\n", name);
}
return 0;
}
scanf is particularly unsuited for user input.
You probably want this:
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Enter %d values\n", n);
for (int i = 0; i < n; i++)
{
// the space at the beginning of "%[^\n]"
// gets rid of the \n which stays in the input buffer
scanf(" %[^\n]", name); // also there sis no 's' at the end of the "%[^\n]" specifier
printf("name = %s\n", name); // for testing purposes
}
}
But this doesn't actually make much sense because the program is asking for n names, but at each run of the for loop the previous name will be overwritten with the new name.
Also be aware that scanf("%[^\n]", name); is problematic because if the user types more than 99 characters you'll get a buffer overflow.

How do you use scanf in a for loop so it does not stop looping on the first try?

for(int i=0;i<num; i++)
{
char word[32];
scanf(" %[^\n]s",word);
makeDictionary(word, readDictionary);
}
I have a program where I want to ask user for certain strings n times (with spacing allowed). However, when they input for example n = 2, it only loops once and exists. I know there is something wrong with my scanf.
I do Java and I'm a beginner at C. The way strings are done is very different.
This code can help you:
#include <stdio.h>
#include <string.h>
int main()
{
int N;
do
{
printf("Give me number of words :");
scanf("%d",&N);
}while(N<1);
for(int i=0;i<N;i++)
{char str[20];
printf("\nGive me the %d word :",i+1);
scanf("%s[^\n]",str);
printf("%s",str);
}
}

Scanning a char array after scanning an integer in C

I'm a newbie in programming. It is confusing me when I taking input of a char array after scanning an integer. It is not working properly.
The code is following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
Output is following:
The problem is due to the white spaces.After scanf("%d",&x); the last entered '\n' character is taken and saved the string a of scanf("%[^\n]",a).
To avoid this give space in scanf() statement
scanf(" %[^\n]",a);//give a space
Why to give a space?
By giving a space,the compiler consumes the '\n' character or any
other white space ('\0','\t' or ' ' ) from the previous scanf()
your code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf(" %[^\n]",a);//give a space
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
fflush(stdin);
scanf("%[^\n]",a);
fflush(stdin);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
This also works. The same goes here, /0 at the end adds up to the character scan and interferes. Using fflush(stdin) will discard any unnecessary input data including the /0.
Correct me if I am wrong as I too am a newbie at coding. :p
Replace scanf("%[^\n]",a); with scanf(" %99[^\n]", a);
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%s",a); // get char array without inputing space
scanf(" %99[^\n]", a); // get char array, allowing inputing space
scanf("%d",&y);
printf("%d\n%s\n%d\n",x,a,y);
return 0;
}
Instead of %d use %d\n to consume the newline, so that the following command will not just read nothing:
scanf("%d\n",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);

Problems with arrays

I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}
printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);
You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);

Strings taken from user in C are being scrambled

I have written the following C code to get in a list of strings from the user. But the stored strings are giving out weird values.
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LENGTH 50
void readInStrings(char* arr[],int n)
{
int i=0;
char line[MAX_STRING_LENGTH];
for(i=0;i<n;i++){
arr[i]=malloc(MAX_STRING_LENGTH);
printf("Enter another string : ");
scanf("%s",&arr[i]);
//fgets(&arr[i],MAX_STRING_LENGTH,stdin);
}
printf("Strings read in correctly.... \n");
printf("Displaying out all the strings: \n");
for(i=0;i<n;i++){
printf("%s\n",&arr[i]);
}
}
void testStringInputs()
{
printf("Enter the number of entries : ");
int n;
scanf("%d",&n);
char* strings[n];
readInStrings(strings,n);
}
Input Sample:
Enter the number of entries : 3
Enter another string : Alladin
Enter another string : Barack Obama
Enter another string : Strings read in correctly....
Displaying out all the strings:
AllaBaraObama
BaraObama
Obama
Problems:
1) Why is one string not taken in as input at all?
2) Why are the displayed strings scrambled like that?
The problem is the same if I use gets() or fgets() in place of scanf().
arr[i] is already a pointer, you don't need the &
Removing the & (as the first answerer noted) in scanf("%s",&arr[i]); and in printf("%s\n",&arr[i]); did the trick for me. Also, note if you compiled with warnings at their highest, your compiler would have told you right away that the & was misplaced.
Its better to use an array of arrays(two dimensional) instead of array of pointers.
I had a tough time correcting your code. So I changed the code to this
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LENGTH 50
void readInStrings(char (*arr)[MAX_STRING_LENGTH],int n)
{
int i;
for(i = 0 ; i< n+1; ++i)
fgets(*(arr+i),MAX_STRING_LENGTH,stdin);
printf("Strings read in correctly.... \n");
printf("Displaying out all the strings: \n");
for(i=0;i< n+1;i++){
printf("%s",arr[i]);
}
}
int main()
{
printf("Enter the number of entries : ");
int n;
scanf("%d",&n);
char strings[n][MAX_STRING_LENGTH];
readInStrings(strings,n);
return 0;
}

Resources