Simple for loop running infinitely - c

I'm new to C and I need help with this simple exercise using for. I need to get a char and an int value from the user. Then I have to print that char as many times as the int entered before.
This is what I have:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a;
printf("Enter a character:");
scanf(" %c", &a);
int n;
printf("Enter a number:");
scanf(" %c", &n);
printf("\n");
int x;
for(x=0; x < n; x++){
printf(" %c", a);
}
return 0;
}
My problem is that it makes an infinite loop in the for.
Please I need your help.
Thanks

Here, n is an int, not a char. Thus, you need to use %d to read it. Using %c here will cause undefined behavior. According to "C99 – ISO 9899-1999":
§7.19.6.2 The fscanf function1
[...] If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.
Change
scanf(" %c", &n);
to
scanf(" %d", &n);
Check out here for more info.
1: The scanf function is equivalent to fscanf with the argument stdin interposed
before the arguments to scanf.

Related

How Can I get input in arrays in C?

#include <stdio.h>
#include <math.h>
int main()
{
int numbers[5];
printf("Enter your first number:%d");
scanf("%d", numbers[0]);
printf("Enter your second number:%d");
scanf("%d", numbers[1]);
numbers[3]=numbers[0]+numbers[1];
printf("Your desired result is:%d",numbers[3]);
return 0;
}
I seem to find no problem in the code but it won't even let me input numbers in the array I declared
The issue is in the second argument of scanf.
scanf("%d", numbers[0]);
it expects an address of where to write your input to but you are passing the value of numbers[0]. So scanf will write to whatever is in numbers[0] which is an int, not a pointer.
Take the address of numbers[0] by changing it to:
scanf("%d", &numbers[0]);

Why is this array bugging when I declare a matrix?

That's the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int linhas=0, col=0, num=0, i=0, pos1[100];
int pos[100];
scanf("%d %d %d", &linhas, &col, &num);
int matriz[linhas][col];
for(i=0; i<num;i++){
scanf(" %c%d", &pos[i], &pos1[i]);
}
for(i=0;i<num;i++){
pos[i] -= 97;
}
return 0;
}
It's quite simple, I declared 2 arrays, one to store the value of a char(pos[]), and the other to store integer values(pos1[]), and it works:D.
The thing is, if I declare a matrix ex: matrix[linhas][col], my code does not really store the values of a char, and if I take it off, it starts to store normally, also, it does not matter whether if I declare the matrix right after getting the rows and colums (linhas and col) or if I declare it at the end of the code. I don't know what the problem is, and I'd appreciate any hints.
int pos[100];
scanf("%d %d %d", &linhas, &col, &num);
int matriz[linhas][col];
for(i=0; i<num;i++){
scanf(" %c%d", &pos[i], &pos1[i]);
}
The %c format specifier will read in a character, but it requires the address of a character to read it into. You pass it the address of an int.
The simplest fix is to change pos to char pos[100];. Another possible fix is this:
for(i=0; i<num;i++){
char c;
scanf(" %c%d", &c, &pos1[i]);
pos[i] = c;
}

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.

Problems with functions

I have two problems with the code, the first one being that the program wants me to enter my number twice and the second one being that the program closes down immediately after it has finished its process.
I have tried to use the getchar() statement to stop it doing so but it doesn't seem to work.
#include <stdio.h>
int square(int); /*function prototype*/
main()
{
int x; /*defining the function*/
printf("Enter your number\n");
scanf_s("%d \n", &x); /*reading the users input*/
printf("Your new answer is %d \n", square(x)); /*calling the function*/
getchar();
getchar();
}
int square(y) /*actual function*/
{
return y * y;
}
Fix the issue by changing
scanf_s("%d \n", &x);
to
scanf_s("%d", &x);
The problem was that a whitespace character (space, newline etc) in the format string of scanf insructs scanf to scan and discard any number of whitespace characters, if any, until the first non-whitespace character.
As for the problem with getchar(), replace the first getchar() with:
int c;
while((c = getchar()) != '\n' && c != EOF);
This will scan and discard everything until a \n or EOF.
Also, change
main()
to
int main(void)
and
int square(y)
to
int square(int y)
I would recommend using scanf("%d", &x); to read your number. Your problem is that your argument looks like this:"%d \n" so the program expects you to enter your number AND \n. This way, you say how you want your x to look, and in your case, it expects it to be a numeric value, a space and end of line.
As for the closing one, use getch();. For this function, you need to include the conio.h like you did with stdio, meaningly: #include <conio.h>.

Why my program combines two printf commands?

So here is my program. It is suposed to write out square of some intiger.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
When i run a program in Eclipse it first requires me to input a number.I put in 5. And then as output it gives me
Type an intiger.Square of that intiger is 25.
It should first print "Type an intiger" and then the rest. But it just combines two printf commands. What is the problem?
You need a newline character - printf("Type an intiger.\n");
In computing, a newline, also known as a line break or end-of-line
(EOL) marker, or simply break, is a special character or sequence of
characters signifying the end of a line of text.
Also format specifier for integer is %d
scanf("%d", &a);
printf("Square of that intiger is %d", a*a);
If you want it on separate lines you can always add '\n' to the string to get a new line.
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.\n");
scanf("%i", &a);
printf("Square of that intiger is %i", a*a);
return 0;
}
There is 2 problem in it. First, if you input the integer, it should be %d. Example :
scanf("%d", &a);
The second, after the input, you should print \n. So, it will be like this printf("\n");. Take a look at my code :
#include <stdio.h>
int main (){
int a;
printf("Type an intiger.");
scanf("%d", &a);
printf("\nSquare of that intiger is %d", a*a);
return 0;
}
In code::blocks it compiles fine anyway put a \n at the end of the first printf and change %i with %d

Resources