Trouble with a C program - c

I had a homework. The condition was: "Make a program in witch will be displayed how many 'A' and 'B' where typed". I tried and this is what i made:
#include <stdio.h>
int main()
{
int n, i, a=0, b=0;
char cr;
scanf ("%i", &n);
for (i=0; i<=n; i++) {scanf ("%c", &cr); if (cr='A') a++; else if (cr=='B') b++;}
printf ("A-%i\n", a);
printf ("B-%i", b);
}
The problem is when i type for example 10, it only lets me type 5 characters, not 10. Why?
The program can be made with for, while or do while.

Problems and solutions
you need to use scanf(" %c", %cr) to prevent the program from reading trailing whitespaces
You should be using if (cr == 'A') and not if (cr = 'A') because you are comparing and not assigning
the loop should be for (i = 0; i < n... not for (i = 0; i <= n.. as the loop would ask for 1+ input from the specified range
#include <stdio.h>
int main() {
int n, a = 0, b = 0;
char cr;
printf("Enter number of tries :\n");
scanf ("%i", &n);
for (int i = 0; i < n; i++) {
printf("Enter char\n");
scanf(" %c", &cr);
if (cr == 'A') a++;
else if (cr == 'B') b++;
}
printf ("A-%i\n", a);
printf ("B-%i", b);
}

Related

Char input does not continue my program as it should? [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 months ago.
I wrote this table writing program in which I wanted to give program ability to continue depending upon char input value but after taking input, Even if input is y the loop still doesnt execute and program moves towards next line
#include <stdio.h>
#include <conio.h>
int main()
{
int T, N, P;
int K = 1;
char ch;
do
{
printf ("\nWhich Number's Table do you want?");
scanf ("%d", &T);
printf ("\nTable should be Uptil?");
scanf ("%d", &N);
do
{
P= T * K;
printf("\n %dx%d = %d", T, K, P);
K= K + 1;
} while(K <= N);
printf("\nDo you want to continue (Y/N)?");
scanf("%c ", &ch);
} while (ch == 'y');
getch();
}
you needgetchar() to skip Enter(\n)
#include <stdio.h>
int main() {
int T, N, P;
int K = 1;
char ch;
do {
printf("\nWhich Number's Table do you want?");
scanf("%d", &T);
getchar();//================here========
printf("\nTable should be Uptil?");
scanf("%d", &N);
getchar();//================here========
do {
P = T * K;
printf("\n %dx%d=%d", T, K, P);
K = K + 1;
} while (K <= N);
printf("\nDo you want to continue (Y/N)?");
scanf("%c", &ch);
getchar();//================here========
} while (ch == 'y');
}
Just changing scanf("%c ", &ch); to scanf(" %c", &ch); fixed my problem.
I guess the character doesnt get read properly just due to the intendation
difference.

How can I take input character in a character array using %c and produce the right output using %s?

This is my code.
#include<stdio.h>
#include<string.h>
int main()
{
int n, i;
char ch[100];
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf(" %c", &ch[i]);
}
printf("%s\n", strupr(ch));
return 0;
}
At first, I want to take the size of the character array in n variable. After, i want to take n character's and assign the array. The output comes from this program is right but it also produce some garbage values.
For example:
5
s d g h f
Output: SDGHFC└U▄■`
How can i ignore the garbage values from my output?
Simply initialize your array ch[] to all zeros. I.E.
for (i = 0; i < 100; i += 1) { ch[i] = '\0'; }
Put this line just after the declaration of ch[].
As you are reading character the spaces you are providing in your input, will also be considered as characters, and strupr(c) will give some shaggy output, also you have to manually provided null character at the end of your character array. Below program might help you find your answer
#include<stdio.h>
#include<string.h>
int main()
{
int n, i;
scanf("%d", &n);
fflush(stdin);
char ch[100];
for(i = 0; i < n; i++){
char temp;
scanf("%c", &temp);
if(temp != '\n')
ch[i] = temp;
else
break;
}
ch[n] = '\0';
printf("%s\n", strupr(ch));
return 0;
}
Your Input should look like
5
sdghf
To give input with spaces. Program will look like.
#include<stdio.h>
#include<string.h>
int main()
{
int n, i;
scanf("%d", &n);
fflush(stdin);
char ch[100];
char temp;
i = 0;
while(scanf("%c", &temp)){
if(temp == ' ')
continue;
if(temp != '\n')
ch[i++] = temp;
else
break;
}
ch[i] = '\0';
printf("%s\n", strupr(ch));
return 0;
}
Now, you can give your character in any arrangement as you want.

C scanf in a for-loop and where to store the input

Using a scanf in a for-loop, where do I store the input, if there is more then 1 loop? Trying to use arrays, but it always fails.
Let's say:
Please enter number of octets: 3
Please enter octet: 1
Please enter octet: 2
Please enter octet: 3
Then the input should be stored in the array, but it doesn't work. For the controll, I printf v[2] and it should be 3, but it is a another number.
#include<stdio.h>
#include<limits.h>
int main()
{
int c;
int v[c];
printf("Please enter number of octets:\n");
scanf("%d", &c);
if ( (c > 1) && (c < CHAR_MAX))
{
for ( i = 1; i <= c; i++)
{
printf("Please enter octet:\n");
scanf("%d", x);
v[c]=x;
}
printf("v[2]: %d\n", v[2]);
}
return 0;
}
You probably want this:
#include <stdio.h>
#include <limits.h>
int main()
{
int c;
printf("Please enter number of octets:\n");
scanf("%d", &c);
if ((c > 1) && (c < CHAR_MAX))
{
int v[c];
// scan values into array
for (int i = 0; i < c; i++) // indexes from 0 to c-1 !!
{
printf("Please enter octet:\n");
scanf("%d", &v[i]);
}
// print all values from array
for (int i = 0; i < c; i++)
{
printf("v[%d]: %d\n", i, v[i]);
}
}
else
{
printf("Input error\n"); // show an error message
}
return 0;
}
Not clear what are you trying to do here but if you modify you code like below it will work:-
for ( i = 0; i <= c; i++)
{
printf("Please enter octet:\n");
scanf("%d", &x);
v[i]=x;
}
printf("v[2]: %d\n", v[2]);
and to print all the value just add below codes
for ( i = 0; i <= c; i++)
printf("v[i]: %d\n", v[i]);
From the code you have posted, the following lines may be the reason for the problem.
int c;
int v[c];
This is invalid. In C, array must be declared like this.
data_type array_name[size];
To make your declaration valid, you must either read the value of n or specify it like this.
int c = 10;
int v[c];
Also,
scanf("%d", x);
Change it to
scanf("%d", &x);
I'll try not to answer the question but let me point out the mistakes
#include<stdio.h>
#include<limits.h>
int main()
{
int c; // uninitialized
int v[c]; // this will behave quite unexpectedly because c is uninitialized
printf("Please enter number of octets:\n");
scanf("%d", &c);
if ( (c > 1) && (c < CHAR_MAX)) // c is a int so why restrict to CHAR_MAX
{
for ( i = 1; i <= c; i++) // trying to scan c elements and i is undefined
{
printf("Please enter octet:\n");
scanf("%d", x);
v[c]=x; // assigning to v[c] everytime so if c is 2 v[2] is the only element getting modified. not something you want to
}
printf("v[2]: %d\n", v[2]);
}
return 0;
}

Why is this reverse string program not working?

I am new to C programming so please do forgive my naivety. The following program when outputted fails to print the last character of the input string as the first character of the output string.
For example:
Enter no. of elements: 5
Enter string: hello
The reversed string is: lleh
Why is the o not printing?
#include <stdio.h>
int main() {
printf("Enter no. of elements: ");
int n;
scanf("%d", &n);
char string[10000];
printf("Enter string: ");
for (int i = 0; i < n; i++) {
scanf("%c", &string[i]);
}
printf("The reversed string is: ");
for (int i = (n - 1); i >= 0; i--) {
printf("%c", string[i]);
}
printf("\n");
return 0;
}
There is a side effect you take care of:
After scanf("%d", &n);, there is a pending newline in the input stream buffer.
When you later input n characters, scanf("%c", &string[i]) first reads the pending newline, then the n-1 first characters you type and the remainder of your input stays in the input buffer.
scanf() is a very clunky function. It is difficult to use properly.
Here is a way to fix your problem:
#include <stdio.h>
int main() {
char string[10000];
int i, n, c;
printf("Enter no. of elements: ");
if (scanf("%d", &n) != 1 || n < 0 || n > 10000)
return 1;
// read and discard pending input
while ((c = getchar()) != '\n' && c != EOF)
continue;
printf("Enter string: ");
for (i = 0; i < n; i++) {
if (scanf("%c", &string[i]) != 1)
break;
}
// the above loop could be replaced with a single call to fread:
// i = fread(string, 1, n, stdin);
printf("The reversed string is: ");
while (i-- > 0) {
printf("%c", string[i]);
}
printf("\n");
return 0;
}
Your scanf() should start with a space( more info about that ). Here is the code:
#include <stdio.h>
int main() {
printf("Enter no. of elements: ");
int n;
scanf(" %d", &n);
char string[10000];
printf("Enter string: ");
for (int i = 0; i < n; i++) {
scanf(" %c", &string[i]);
}
/* Just to be safer. */
string[n] = '\0';
printf("The reversed string is: ");
for (int i = (n-1); i >= 0; i--) {
printf("%c", string[i]);
}
printf("\n");
return 0;
}
Adding the space to the format string enables scanf to consume the
newline character from the input that happens everytime you press
return. Without the space, string[i] will receive the
char '\n'
So, merely one space is put before format specifier %c at line 11.
scanf(" %c", &string[i]);

A right angle triangle of that symbol with sides equal to that number

The program I'm working on outputs a right triangle of that symbol with sides equal to that number. What it should do is terminate if you enter 0 otherwise it should ask for a new input again.
So my question is how can I make it terminate if you enter 0, otherwise ask for another input? I know I probably need to use while loop. But how do I change it?
Here is my code:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s; /*s is symbol (the input)*/
int a, b, n; /*n is number of rows (the input)*/
printf("Please type the symbol of the triangle:...\n"); /*Ask for symbol input*/
scanf_s("%c", &s, 1);
printf("Please type a positive non-zero number between 5 and 35:...\n"); /*Ask for number of rows input*/
scanf_s("%d", &n);
assert(n >= 5 && n <= 35);
for (a = 1; a <= n; a++) /*How many rows to display+create*/
{
for (b = 1; b <= a; b++)
{
printf("%c", s);
}
printf("\n");
}
system("PAUSE");
}
You can use loops to do that.
#include <stdio.h>
#include <stdlib.h>
#ifndef _MSC_VER
/* passing extra arguments to scanf is not harmful */
#define scanf_s scanf
#endif
int main(void)
{
char s; /*s is symbol (the input)*/
int a, b, n; /*n is number of rows (the input)*/
do {
printf("Please type the symbol of the triangle:...\n"); /*Ask for symbol input*/
scanf_s("%c", &s, 1);
printf("Please type a positive non-zero number between 5 and 35:...\n"); /*Ask for number of rows input*/
scanf_s("%d", &n);
if(n >= 5 && n <= 35)
{
for (a = 1; a <= n; a++)/*How many rows to display+create*/
{
for (b = 1; b <= a; b++)
{
printf("%c", s);
}
printf("\n");
}
}
while ((a = getchar()) != '\n' && a != EOF); /* remove the newline character from standard input buffer */
} while (n != 0);
system("PAUSE");
}

Resources