473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

c getchar help

Howdy. I'm very new to C, so most of my questions will probably be "you dont know that how?!?!" kinds, but I'm trying to learn

What I am trying to do is make it so at the end of the program, it asks the user if he would like to run it again (Y/N).

Here is what I have thought about to make that work. It would obiously be a do loop. Then have getchar read either Y or N and assign Y=1, N=0. After that, have the program read what char was inputed, and either run the loop again, or close the program.

However, I have NO idea how to do that in code. Any help?

Thanks in advanced
Oct 18 '07 #1
7 2231
oler1s
671 Expert 512MB
Everyone starts out in a state of ignorance. The trouble arises when people come here, but don’t make any effort on their part. But on to your question.

It would obiously be a do loop.
You could make it work with other loops as well, but using a do-while loop is the most straightforward and best option.

Then have getchar read either Y or N and assign Y=1, N=0.
That’s too much unnecessary work. Do you want to repeat? The only answer you care about is “Yes”. Anything else and you assume no. Well, you could make this more robust, but as a beginner, start out simple and build up from there. So all you have to do is have getchar read a character. Whatever the user types in. In the end, you’ll compare that character to a Y, to determine whether the program should repeat.

After that, have the program read what char was inputed,
Why? You just read in the input with getchar…

How do you code this? Go through it step by step. First, write down the code for a do/while loop. That’s it. Just create a do while loop. Forget about reading a char or anything. So that’s your assignment for now. Post code that shows a do while loop. Make sure it can compile.
Oct 18 '07 #2
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6. int value, rDigit;
  7.  
  8. printf("Enter a number to be reversed.\n");
  9. scanf("%d", &value);
  10.  
  11. do
  12. {
  13.     rDigit = value % 10;
  14.     printf("%d", rDigit);
  15.     value = value / 10;
  16. } while (value != 0);
  17.  
  18. printf("\n");
  19. system("pause");
  20. return 0;
  21. }
  22.  
Oct 19 '07 #3
mattmao
121 100+
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.  
  6. int value, rDigit;
  7.  
  8. printf("Enter a number to be reversed.\n");
  9. scanf("%d", &value);
  10.  
  11. do
  12. {
  13.     rDigit = value % 10;
  14.     printf("%d", rDigit);
  15.     value = value / 10;
  16. } while (value != 0);
  17.  
  18. printf("\n");
  19. system("pause");
  20. return 0;
  21. }
  22.  

If you comment this line then your code works fine.
Expand|Select|Wrap|Line Numbers
  1. system("pause");
Oct 19 '07 #4
If you comment this line then your code works fine.
Expand|Select|Wrap|Line Numbers
  1. system("pause");
That's not the code I need it in though. That's just an example of a do while loop. Here is the code I need to put it in:

Expand|Select|Wrap|Line Numbers
  1. /*******************************************************************************
  2. * Name: tripAdvanced.c
  3. * Assignment: LAB 2
  4. * Date Written: 10/11/2007
  5. * Course: CS133
  6. * Purpose: Calculate more usefull info for a car trip
  7. * Sources:
  8. *******************************************************************************/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. int main()
  13. {
  14.     printf("Welcome to the Trip Planner!\n"
  15.     "So you are ready to take a trip? Let me help you plan for\n"
  16.     "your fuels costs and required stops to fill up your tank.\n"
  17.     "===========================================================================\n"
  18.     "Please provide answers to the prompts below and I will\n"
  19.     "display a summary for you when I have computed the results.\n"
  20.     "===========================================================================\n"
  21.     "\n");
  22.  
  23. float MPG, Lcost, Hcost, miles, gallons, LTcost, HTcost;
  24.  
  25.     printf("Please input your car's average miles per gallon >> ");
  26.     scanf("%f", &MPG);
  27.     printf("Please tell me the range of fuel costs you expect to pay (per gallon).\n"
  28.     "The lowest per gallon cost of fuel is >> ");
  29.     scanf("%f", &Lcost);
  30.     printf("The highest per gallon cost of fuel is >> ");
  31.     scanf("%f", &Hcost);
  32.     printf("Please tell me how many miles you plan to travel >> ");
  33.     scanf("%f", &miles);
  34.  
  35.     gallons= miles / MPG;
  36.     LTcost= gallons * Lcost;
  37.     HTcost= gallons * Hcost;
  38.  
  39.     printf("\n"
  40.     "===============   Trip Summary   =================="
  41.     "\n"
  42.     "\n");
  43.  
  44.     printf("You will need to purchase %.2f gallons of fuel.\n" , gallons);
  45.     printf("The approximate cost of fuel for your trip is between $%.2f and $%.2f\n" , LTcost, HTcost);
  46.  
  47. system("pause");
  48. return 0;
  49. }
  50.  
Oct 19 '07 #5
sicarie
4,677 Expert Mod 4TB
Okay, so what was your question there? Did you try it? Did you get it to work (I'm guessing no as you are posting here, but what didn't work)?
Oct 19 '07 #6
Okay, so what was your question there? Did you try it? Did you get it to work (I'm guessing no as you are posting here, but what didn't work)?
I don't want to use a number to exit the loop. I want to prompt the user to hit N to exit, or Y to run the loop again. I just don't know how to do that code wise.
Oct 19 '07 #7
oler1s
671 Expert 512MB
I don't want to use a number to exit the loop. I want to prompt the user to hit N to exit, or Y to run the loop again. I just don't know how to do that code wise.
Ok, so take that sample code and too it. You know your while loop involves checking if the user hit "Y". So clearly, the condition is a comparison between a character and 'Y'. Except, now you need a character that holds user input.

So create a variable to hold that character. Now in your do while loop, you clearly need to ask your user the question, and have him input a character. Write the appropriate code. First the variable. Then the question. Then the input.

Take initiative here. Make an attempt!
Oct 19 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets...
6
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always...
1
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
25
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
20
by: Senthil-Raja | last post by:
The getchar() function is expected to fetch the next character in the input stream and return it. But, when I wrote a program using this function, it looks like the reading of the input stream...
9
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
3
by: mahiapkum | last post by:
Hello, I have a code which uses getchar(). #include<stdio.h> int main() { char ch; ch = getchar(); if(ch == 'Y') { printf("Beautiful...
22
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.