c getchar help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • THOR4LIFE
    New Member
    • Oct 2007
    • 4

    #1

    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
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    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.

    Comment

    • THOR4LIFE
      New Member
      • Oct 2007
      • 4

      #3
      Code:
      #include <stdio.h>
      
      int main()
      {
      
      int value, rDigit;
      
      printf("Enter a number to be reversed.\n");
      scanf("%d", &value);
      
      do
      {
          rDigit = value % 10;
          printf("%d", rDigit);
          value = value / 10;
      } while (value != 0);
      
      printf("\n");
      system("pause");
      return 0;
      }

      Comment

      • mattmao
        New Member
        • Aug 2007
        • 121

        #4
        Originally posted by THOR4LIFE
        Code:
        #include <stdio.h>
        
        int main()
        {
        
        int value, rDigit;
        
        printf("Enter a number to be reversed.\n");
        scanf("%d", &value);
        
        do
        {
            rDigit = value % 10;
            printf("%d", rDigit);
            value = value / 10;
        } while (value != 0);
        
        printf("\n");
        system("pause");
        return 0;
        }

        If you comment this line then your code works fine.
        Code:
        system("pause");

        Comment

        • THOR4LIFE
          New Member
          • Oct 2007
          • 4

          #5
          Originally posted by mattmao
          If you comment this line then your code works fine.
          Code:
          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:

          Code:
          /*******************************************************************************
          * Name: tripAdvanced.c
          * Assignment: LAB 2
          * Date Written: 10/11/2007
          * Course: CS133
          * Purpose: Calculate more usefull info for a car trip
          * Sources:
          *******************************************************************************/
          #include <stdio.h>
          #include <stdlib.h>
          
          int main()
          {
              printf("Welcome to the Trip Planner!\n"
              "So you are ready to take a trip? Let me help you plan for\n"
              "your fuels costs and required stops to fill up your tank.\n"
              "===========================================================================\n"
              "Please provide answers to the prompts below and I will\n"
              "display a summary for you when I have computed the results.\n"
              "===========================================================================\n"
              "\n");
              
          float MPG, Lcost, Hcost, miles, gallons, LTcost, HTcost;
                 
              printf("Please input your car's average miles per gallon >> ");
              scanf("%f", &MPG);
              printf("Please tell me the range of fuel costs you expect to pay (per gallon).\n"
              "The lowest per gallon cost of fuel is >> ");
              scanf("%f", &Lcost);
              printf("The highest per gallon cost of fuel is >> ");
              scanf("%f", &Hcost);
              printf("Please tell me how many miles you plan to travel >> ");
              scanf("%f", &miles);
              
              gallons= miles / MPG;
              LTcost= gallons * Lcost;
              HTcost= gallons * Hcost;
                  
              printf("\n"
              "===============   Trip Summary   =================="
              "\n"
              "\n");
              
              printf("You will need to purchase %.2f gallons of fuel.\n" , gallons);
              printf("The approximate cost of fuel for your trip is between $%.2f and $%.2f\n" , LTcost, HTcost);
              
          system("pause");
          return 0;
          }

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #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)?

            Comment

            • THOR4LIFE
              New Member
              • Oct 2007
              • 4

              #7
              Originally posted by sicarie
              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.

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                Originally posted by THOR4LIFE
                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!

                Comment

                Working...