Thread: Help with inputing multiple char variables

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Help with inputing multiple char variables

    Hello all,

    I'm new here and am pretty much a rookie programmer. I'm working on a home project that saves recipes and will eventually sort them. I need help with the cin command on multiple variables. Here is my code:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        char name, category, group, style, prep, ingred, instruct, notes;
    
        //Recipe information questions:
    
        cout<<"Recipe name? \n ";
        cin>> name;
        cin.ignore();
        cout<<"Category? \n";
        cin>> category;
        cout<<"Food Group? \n";
        cin>> group;
        cin.ignore();
        cout<<"Food Style? \n";
        cin>> style;
        cin.ignore();
        cout<<"Prep Time? \n\n";
        cin>> prep;
        cin.ignore();
        cout<<"Recipe Ingredients? \n\n";
        cin>> ingred;
        cin.ignore();
        cout<<"Cooking Instructions? \n\n";
        cin>> instruct;
        cin.ignore();
        cout<<"Serving Notes? \n\n";
        cin>> notes;
        cin.ignore();
    
    
    }
    I attached a picture of what happens after I run it. After I type in Chicken Parm and hit enter it wont let me enter the other variables. Do I need to use strings? Help!

    Thanks,

    Tony

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It looks like you want to enter in strings, not chars.

    See this thread for an example on how to get a string from the user.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    I'm playing around with my code right now and when I put in an answer (using string variables) I think it has a problem with spaces. I want it to work something like this:

    Name?
    Chicken Parm

    Category?
    Entree

    Food Group?
    Meats

    Food Style?
    Italian

    Prep Time?
    15 minutes

    Ingredients?
    Chicken
    breading
    2 eggs.....

    Instructions?
    Bread chicken
    Cook on 7 for half an hour....

    Serving notes?
    Serve hot

    I want it to store all this, and after I'm going to include an if statement "do you want to view the recipe? y/n?" and then I want it to display it back.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Post your code which is giving you the problem.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    The code is posted, I'm reading up on strings and just ordered a C++ book online. I only learned a little C programming in school. I have a great idea for a project at home and am excited to get it working.

    Thanks for the help.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by piertony2000 View Post
    The code is posted, I'm reading up on strings and just ordered a C++ book online. I only learned a little C programming in school. I have a great idea for a project at home and am excited to get it working.

    Thanks for the help.
    Well, you said "when I put in an answer (using string variables) I think it has a problem with spaces". So you obviously modified your code to use strings like I suggested, but I can't help you with the "spaces" problem without seeing your new code.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    o sorry: new code:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string name, category, group, style, prep, ingred, instruct, notes;
        char answer;
    
        //Recipe information questions:
    
        cout<<"Recipe name? \n\n ";
        cin>> name;
        cin.ignore();
    
        cout<<"Category? \n\n";
        cin>> category;
        cin.ignore();
    
        cout<<"Food Group? \n\n";
        cin>> group;
        cin.ignore();
    
        cout<<"Food Style? \n\n";
        cin>> style;
        cin.ignore();
    
        cout<<"Prep Time? \n\n";
        cin>> prep;
        cin.ignore();
    
        cout<<"Recipe Ingredients? \n\n";
        cin>> ingred;
        cin.ignore();
    
        cout<<"Cooking Instructions? \n\n";
        cin>> instruct;
        cin.ignore();
    
        cout<<"Serving Notes? \n\n";
        cin>> notes;
        cin.ignore();
        
        cout<<"View Recipe? y/n? \n\n";
        cin>> answer;
        cin.ignore();
        
        
    
    
    }

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Read the link I posted. You want to use the getline() function so that it won't break your string at a space.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    thanks bithub. I'm reading up now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 12:41 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM