My code has 3 functions doing 3 different works. First one just prints "Hello!", second one takes an integer input from the user and returns its square and the third one takes a character input from the user and makes a triangular pattern out of it. But when I execute the code the first two functions work well and the third one doesn't. Although, when I execute the third function separately, it works well. What could be the problem?
Here is the code :
#include <stdio.h>
void greet()
{
printf("Hello !\n");
}
int square()
{
int n;
printf("Enter a number to be squared:\n");
scanf("%d",&n);
return n*n;
}
void pattern(char ch)
{
printf("Enter a Character:\n");
scanf("%c",&ch);
for (int i=10;i>0;i--)
{
for (int j=0;j<i;j++)
{
printf("%c",ch);
}
printf("\n");
}
}
int main()
{
int num;
char c;
greet();
num=square();
printf("%d\n",num);
pattern(c);
return 0;
}
Because a user is hitting the "Enter" key to submit their character for the second function, that "Enter" character is being passed on to the third function's scanf, and as such will print a triangle of new-lines (or a column in this case, you probably got a large amount of whitespace printed after the program's execution).
You can fix it by including a space before the %c in the pattern func's scanf, like this:
void pattern(char ch) {
printf("Enter a Character:\n");
scanf(" %c",&ch);
for (int i=10;i>0;i--)
{
for (int j=0;j<i;j++)
{
printf("%c",ch);
}
printf("\n");
}
}
Related
I am writing a program like hangman in c and I am trying to run it .The problem is that it's working fine until I give it a letter to quess the word but then it crashes with -1073741819 (0xC0000005). Can someone help me solve this, I think its something really small that I cant
see . Thank you for helping me!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Rules(void);
void maskWord (char starword[], int size);
int playRound(char starword[], char answer[]);
void updateStarWord(char starword[], char answer[], char userguess);
int occurancesInWord(char userguess, char answer[]);
int c=0;
char answer[128];
int N;
int main()
{
int ch,size;
char starword[1000];
do
{
printf("---Welcome to Hangman!---\n");
printf("1.Start Game\n2.Instructions\n3.Exit\n");
scanf("%d",&ch);
if(ch>0&& ch<4)
{
switch(ch)
{
case 1:
maskWord(starword,size);break;
case 2:
Rules();break;
}
}
else
system("cls");
}
while(ch!=3);
return 0;
}
void Rules(void)
{
do
{
do
{
printf("\nThe word to guess is represented by a row of dashes representing each letter of the word.");
printf("\nRules may permit or forbid proper nouns, such as names, places, brands, or slang.");
printf("\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions.");
printf("\nIf the suggested letter does not occur in the word, the other player draws one element of a hanged stick figure as a tally mark.\n");
}
while(getchar()!='\n');
}
while(getchar()!='\n');
}
void maskWord (char starword[], int size)
{
printf("Enter word to guess: ");
fflush(stdout);
scanf(" %s", answer);
int N = strlen(answer);
int mask[N];
for (int i=0; i < N; ++i)
{
mask[i] = 0;
}
playRound(mask,N);
}
int playRound(char starword[], char answer[])
{
// Loop over each round of guessing
int gameover = 0;
while (! gameover)
{
// Print word with *s for unguessed letters
printf("The word is : ");
for(int j=0; j < answer; ++j)
{
if (starword[j])
{
printf("%c", answer[j]);
}
else
{
printf("*");
}
}
printf("\n");
// Get player's next guess
char guess;
printf("\nGive a letter: ");
fflush(stdout);
scanf(" %c", &guess);
updateStarWord(starword,answer,guess);
}
}
void updateStarWord(char starword[], char answer[], char userguess)
{
// Mark true all mask positions corresponding to guess
int k;
for(k=0; k < answer; ++k)
{
if ((answer[k]) ==(userguess))
{
starword[k] = 1;
}
}
}
Your for loop doesn't make sense because the condition for terminating it is k < answer. You are comparing an integer (k) to a pointer (answer). The compiler should have warned you about this, so make sure your compiler warnings are turned on and you are paying attention to them. Pointers and integers are different things, and comparing them is almost never what you want to do.
If answer is null-terminated, you could probably replace that condition with answer[k]. Or maybe updateStarWord needs to take an argument that indicates the length of answer, and then the condition would be k < answer_length.
#include<stdio.h>
int main(){
char name[3];
float price[3];
int i,page[3];
printf("enter the name price and book\n");
for(i=0;i<3;i++){
scanf("%s",name[i]);
printf("enter character:\n");
}
for(i=0;i<3;i++) {
scanf("%f",&price[i]);
printf("enter floating point number:\n");
}
for(i=0;i<3;i++) {
scanf("%d",&page[i]);
printf("enter digit:\n");
}
printf("\n");
for(i=0;i<3;i++) {
printf("%s\n",name[i]);
}
for(i=0;i<3;i++) {
printf("%f\n",&price[3]);
}
for(i=0;i<3;i++){
printf("%d\n",&page[i]);
}
return 0;
}
When I was trying this code, I thought this was quite simple to do but I realized, there is something which I am missing in the code. The main problem with this code is it is not scanning the values of price and pages. I don't understand where I am mistaken.
So, please correct my code so that it will print the values of name price and pages.
There are a number of issues in your code. First and foremost, the %s specifier (for both the scanf and printf functions) expects a string argument (that is, a nul-terminated array of char for printf or an array sufficiently large to hold the input characters plus that terminator, for scanf); however, you are attempting to read (and print) a single char value in each of the relevant for loops.
To fix this, use the %c format specifier, instead of %s. However, when you use this, the newline character that is generated when you press the Enter key will be left in the input buffer, and that will be read as the actual char input on the next iteration of the first for loop. To clear any such newline (or, indeed other whitespace) from the input before the real input, add a space in the format string before the %c. Also, when using this, you will need to pass the address of each name element: scanf(" %c", &name[i]);.
Further, the printf function takes the actual values of the variables to be output, rather than their addresses – so remove the & from the arguments in your printf calls. (Also, and I assume it's a typo, the price[3] expression should be price[i] – the former attempts to access an out-of-bounds element of the price array.)
Another issue is that, in each of your input loops, you call the scanf function before you display the relevant prompt. In the code below, I have reversed your printf and scanf lines in each of those input loops.
Here's a possible fixed version:
#include<stdio.h>
int main()
{
char name[3];
float price[3];
int i, page[3];
printf("enter the name price and book\n");
for (i = 0; i < 3; i++) {
printf("enter character:\n");
scanf(" %c", &name[i]);
}
for (i = 0; i < 3; i++) {
printf("enter floating point number:\n");
scanf("%f", &price[i]);
}
for (i = 0; i < 3; i++) {
printf("enter digit:\n");
scanf("%d", &page[i]);
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("%c\n", name[i]);
}
for (i = 0; i < 3; i++) {
printf("%f\n", price[i]);
}
for (i = 0; i < 3; i++) {
printf("%d\n", page[i]);
}
return 0;
}
Is there any way to Count number of arguments passed to scanf() in C ? Specially, while assigning int arrays through scanf().
Example:
int array[1000], i;
for(i=0;i<1000;i++)
scanf("%d",&array[i]);
I need to count how many values are inserted by user
I don't think there's a built in way to do this, but why not just create a counter that increments when scanf returns successfully and break the loop otherwise?
int scanf_counter = 0;
int array[1000], i;
for(i=0;i<1000;i++) {
if(scanf("%d",&array[i] > 0) {
scanf_counter++;
} else {
break;
}
}
Although I'm not sure I understand your question exactly because you could always just find the size of the array by doing this
int size = sizeof(array)/sizeof(array[0])
Look the scanf() fragment carefully, thus:
include
int main()
{
double a[100000],mx=0;
int i,j,c=0;
printf("Enter as many numbers as you wish . Press Ctrl+D or any character to stop inputting :\n");
for(i=0;i<100000;i++)
{
if((scanf("%lf",&a[i]))==1)
c++;
//else break;
}
for(j=0;j<c;j++)
{
if(a[j]>mx) mx=a[j];
}
printf("You have inserted %d values and the maximum is:%g",c,mx);
return 0;
}
I'm having problems while using gets in C.
...
int main()
{
char test[20], m[20];
int n;
scanf("%d", &n);
while(n)
{
gets(test);
test.kolona = n;
m = decode(test); //some function
printf("%s",m.sif);
putchar('\n');
scanf("%d", &n);
}
}
When I enter a number and press enter, it automatically "prints" a newline, before you input the string. I searched a bit and found that this can be avoided if you put a gets before, like this:
...
scanf("%d", &n);
gets(test)
while(n);
{
gets(test);
...
}
But then it messes up again as the loop continues :(
Is there an elegant solution to this?
sample to fix
int main()
{
char test[20], m[20];
int n;
scanf("%d%*c", &n);//skip the newline following the numeric input
while(n)
{
scanf("%19[\^n]", test);//gets has been abolished.
//test.kolona = n;//The array does not have a member field.
//strcpy(m, decode(test));//m = decode(test); //Can not be assigned to the array in this way
printf("%s\n", m);
//putchar('\n');
scanf("%d%*c", &n);
}
}
I'm currently trying to write a C program using the NetBeans IDE.The goal is to take input from the user on the amount of sticks they want drawn on the screen where the Letter "I' is a stick as noted in the for loops. The thing is, I use scanf to get the user input for each row , but the program executes and runs but never actually seems to reach the for loop, since it doesn't do any of the print statements within it. My IDE doesn't print any of my print statements in the for-loops, which is why I'm assuming it isn't reaching them.
I have tried simply assuming values for user input, instead of using scanf, but that also does not seem to reach the for loop. Any input on what I could do to fix this, would be much appreciated!
#include <stdio.h>
int main(void) {
int length1, length2, length3;
int inputRow1, inputRow2, inputRow3;
printf("Please enter the number of Sticks you would like per row\n");
scanf("%d%d%d", inputRow1, inputRow2, inputRow3);
for (length1 = 0; length1 < inputRow1; length1++) {
printf("I");
for (length2 = 0; length2 < inputRow2; length2++) {
printf("I");
for (length3 = 0; length3 < inputRow3; length3++) {
printf("I");
return (0);
}
}
}
}
While using scanf you should store the values in the variables using &.
Change
scanf("%d%d%d",inputRow1,inputRow2,inputRow3);
to
scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);
And your return statement should be before exiting from the main().
For example:
#include <stdio.h>
int main()
{
int length1,length2,length3;
int inputRow1,inputRow2,inputRow3;
printf("Please enter the number of Sticks you would like per row\n");
scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);
for(length1=0;length1<inputRow1; length1++)
{
printf("I");
for(length2=0;length2<inputRow2;length2++)
{
printf("I");
for(length3=0;length3<inputRow3;length3++)
{
printf("I");
}
printf("\n");
}
printf("\n");
}
printf("\n");
return(0);
}