I am an absolute beginner to programming and I am starting with the C language. I am currently using the Beginning Programming with C for Dummies book by Dan Gookin.
When doing an exercise with fgets() the following occurred.
This is my code
#include <stdio.h>
int main()
{
char name[10];
printf("Who are you? ");
fgets(name,10,stdin);
printf("Glad to meet you, %s.\n",name);
return(0);
}
The expected result should be a name with a full stop at the end and what is happening is that the full stop carries over to the next line like shown below.
I am using the code blocks IDE on Ubuntu
You typed Miguel and then pressed ENTER. name now holds Miguel\n, in which \n stands for the ENTER you just pressed, since ENTER counts as a character too. If you want to remove it, you'll find the answer here.
You can use strtok(name, "\n")
Related
I'm currently in the very early stages of learning C programming and am working my way through "Beginning Programming with C for Dummies" using Code::Blocks.
The first activity in Chapter 7, Fetching characters with getchar(), asks us to copy the code exactly as it's presented in the book; see below:
#include <stdio.h>
int main()
{
int c;
printf("I'm waiting for a character: ");
c = getchar();
printf("I waited for the '%c' character.\n", c);
return (0);
}
The output I get is:
I'm waiting for a character:
However according to the book, the output that I should be seeing is the character's ASCII code value. It then asks that I change the %c placeholder to %d to display the value, but still I get the same outcome as before. I could probably recite the code with my eyes closed I've checked it through that may times; I simply cannot see where I'm going wrong.
Am I right in thinking that the getchar() function isn't being recognized? Or that the code isn't being read after the first printf statement? Any guidance is welcome as I don't want to move on until I've understood the problem.
Please enter any key, then 2nd printf will show the result. getchar() is expecting input from user, controller reach at 2nd line & waiting for input.
I need some help on my exercise.
Question of exercise
And this was my code:
#include <stdio.h>
main()
{
int price,new_price;
char code;
printf("Enter the price:");
scanf("%d",&price);
printf("Enter the pricing code:");
scanf("%s",&code);
if(code == 'A')
{
new_price=price*0.5;
printf("New discounted price is $%d.00",new_price);
}
else if(code =='B')
{
new_price=price*0.6;
printf("New discounted price is $%d.00",new_price);
}
}
I know that scanf("%c") gets skipped after input of scanf("%d"), so when I asked my lecturer for a solution , she told me to change scanf("%c") to scanf("%s"). THe problem is , I cannot obtain the new discounted price. The value would show up as $0.00.
This image sums up my problem
The value shows up as $0.00 even though the code runs
I tried using switch and the code works perfectly fine, but I just want to figure out a way to make this code work without using scanf(" %c") formatter.. Any help would be greatly appreciated ! Thanks in advance !
You put in "100<enter>A". Your code is designed to read an integer followed by a character. So it read in "100" and then "<enter>". You should have put in "100A<enter>" if your code is setup to read an integer followed by a character.
If you want to read lines of input and parse them, write code that does that. You can use such a function after you read the integer to read the line ending after it. Then you can read the character.
Hi I just started learning C programming in gcc compiler on my Debian system. Here is the code
main()
{
fflush( stdin );
int a,b;
scanf("%d,%d",&a,&b);
printf("%d,%d",a,b);
}
The scanf doesn't take input for the second variable. I press 2 and then return key and it displays
root#debian:/home/wis# ./test
2
2,0root#debian:/home/wis#
I have used space and tab key also. Please help me.
You defined your scanf string as "%d,%d", so the program expect an input like 1,2.
If you give it only one digit and press Enter, it parses the first digit and leaves the second one untouched. It was assigned 0 on declaration, so that's what you are seeing when printing.
Your printf statement would benefit from an "\n" at the end, and your code snippet needs indentation. Please show your includes (#include <stdio.h>) next time, it makes it easier for us to compile and run the code.
I'm trying to compile the following code. When I give input to the program then after pressing enter a popup appears which shows that
store program.exe has stopped working
Note: I am using Windows 8.1
Note: I am working on a program (which used in super stores), which includes the following things:
Product code
Product name
Product price
Total bill calculations
It's just the starting.
#include <stdio.h>
int main (void)
{
int d, code;
char product[100], price[100];
printf("\t\t Welcome to the Metro Store\n\n\n\n Enter your product code: ");
scanf("%d",code);
if(code<100)
printf("Pharmacy\n Name of the Medicine");
fflush(stdout);
fgets(product, 100, stdin);
printf(product);
return 0;
}
For starters you should try
scanf("%d", &code);
You have to tell scanf where to write to. If you don't specify the ampersand (&), scanf will not know where is should write to.
You should read the docs and definitely a good introduction to pointers. If you don't understand pointers, programming in C and C++ is pointless ;-)
Then you can change your fgets() to scanf( "%s", product );
In this case scanf does not need the & because product is short for &product[0]. This can get rather confusing, so get to grips with pointers before continuing.
First, scanf() expects a pointer type variable as as an argument to the format specifier. Use
scanf("%d", &code);
^^
Second, do not mix up scanf() and fgets(). Otherwise, the fgets() will end up only consuming the newline left by scanf("%d"..). Try to use fgets() for taking user input to be on safer side. However, if you have to use both, use something like
scanf("%d", &code);
int ch; while ((ch = getchar())!= EOF && ch != '\n');
fgets(product,100,stdin);
to avoid the issue with the leftover newline.
I'm a middle experienced Java developer and have many problems learning the C language for my computer science study. I try it with the book "The C Programming Language" which many people seem to recommend.
But I've got problems with the simplest stuff like the EOF in combination with getchar(). Here's the code:
#include<stdio.h>
main()
{
int i = 0;
while (getchar() != EOF)
{
++i;
printf("Count of characters is %d", i);
}
}
I'm working with Mac OS X Lion and use the "cc" command with "./a.out" for running in terminal, like described in the book to run the file. And what I get is:
Always counting one character too much
the while loop never ends! it just waits for another input after reaching end of input ...
I really have no idea what could be the issue. Can someone help?
Always counting one character too much
That could be the newline (enter / return).
the while loop never ends! it just waits for another input after
reaching end of input
You are likely not signaling end of input. You should be using CTRL-D to do so.
When you type a character, such as "6" and you click enter (which is equal to \n), then the command "6\n" is sent, so it is 2 characters. If you just press enter, then 'i' will be increased by 1.
The EOF means end of file and its equivalent to ctrld+D. It is useful if you read a text file. Else it is the same as saying "Forever".