Thread: Assigning variables and if else statements

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    4

    Assigning variables and if else statements

    Hello, full disclosure this is for a homework assignment for my C programming class and any help would be greatly appreciated!


    So I have to set up a program specifying certain packages for a cruise line, inputting number of passengers and their ages with possible discounts for minors. However, I'm having trouble starting the code, because I can't find the right combination of getchar(), or scanf(%d,&variable) that allows the user to input a character, leading them to the right part of the program that applies to them. For instance, I'd like the user to input R or M, depending on whether they want the room or the room and meal package. They input the variable, and that sets my x value equal to R or M, leading them down the right if and else codes. They input R, so I want different things to happen than if they inputted M. Is there an easier way to do this than the code I set up below? If not, how do I use a user inputted variable to direct where my code goes? In the example I posted, the code always returns "failed", and I am trying to get it to return number of passengers.

    Code:
    #include <stdio.h> 
    //, Homework 1, Question 8
    int main()
    {
      int R, M;
      int p;
      int c;
      int x;
    
    
      printf("Enter R for room package\nEnter M for room plus meals package\n");
      x = getchar();
      getchar(); //this line is so enter wont count as a character. 
      
       if ( x == R )
         {
            printf("Number of passengers?\n");
          }
      else
        {
             printf("failed");
         }
         
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,708
    Try
    if ( x == 'R' )

    A variable called R has nothing to do with a character with a value of 'R'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    checking letter?
    Code:
    char x;
    if ( x == 'R')
    printf("yep\n");
    else
    printf("nope\n");

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    Thank you! setting it as a character helped, now I'm going to try and write the code with that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning values to variables, a noobie question
    By patishi in forum C Programming
    Replies: 8
    Last Post: 09-28-2013, 07:36 PM
  2. Assigning variables to elements of structs
    By wayne188 in forum C Programming
    Replies: 4
    Last Post: 03-09-2010, 06:37 AM
  3. Assigning variables to strings
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2006, 06:39 PM
  4. assigning rand outputs to variables
    By 2fastwrx in forum C++ Programming
    Replies: 8
    Last Post: 09-27-2004, 10:17 PM
  5. i/o assigning variables
    By bballzone in forum C++ Programming
    Replies: 4
    Last Post: 07-30-2004, 10:49 AM

Tags for this Thread