Spaces in Character Arrays

This is a discussion on Spaces in Character Arrays within the C++ Programming forums, part of the General Programming Boards category; im using dev-c++ and have a string declared like this char itemname[26][50]; this is how the user inputs data into ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Spaces in Character Arrays

    im using dev-c++ and have a string declared like this

    char itemname[26][50];


    this is how the user inputs data into the string

    cout << "Enter the Name of Item " << x << ": ";
    cin >> itemname[x];
    cout << endl;



    if the user inputs a space somewhere, e.g. "X Y" the program pretty much stops working and refuses to accept any more inputs.


    am i doing something wrong? anything else i can do?

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    use:

    cin.getline(intemname[x],'\n');
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Lynux-Penguin
    use:

    cin.getline(intemname[x],'\n');
    Correct me if I'm wrong.
    If he wants to input the whole sting, I think it should be cin.getline(intemname, '\n');

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    2
    It works but then it prevents me from adding anything else to the array... the entire thing is in a loop

    Code:
    do
    
     {
          x++;
    
          cout << "Enter the Name of Item " << x << ": ";
          cin.getline(itemname[x],'\n');
    
          cout << "Enter the Price of Item " << x << ": $";
          cin >> itemprice[x];
          cout << endl;
    
          cout << "Is the Item Taxable (y/n)   ";
          cin >> taxyn[1][x];
          cout << endl;
    
          cout << endl;
    
          stotal = stotal + itemprice[x];
    
          if (taxyn[1][x] != 'n')
          {
             cout << "%";
             gst = gst + (stotal * 0.1);
          }
    
          if (taxyn[1][x] = 'n')
          {
             cout << "";
             gst = gst + (stotal * 0.0);
          }
    
          cout << itemname[x] << "\t\t$" << itemprice[x] << endl << endl;
    
          cout << "Do you wish to add another Item? (y/n)    ";
          cin >> yn;
          cout << endl;
    
          if (x == 100)
           {
             cout << "Cannot Add any more Items!" << endl;
             yn = 'n';
           }
           cout << endl;
    
           system("PAUSE");
           //clrscr();
    
      } while (yn == 'y' || yn == 'Y');


    It only lets me add one thing to itemname the first time the loop executes but after that, it just skips that step all together.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  2. scanf(), Parameter Passing, and Character Arrays
    By linguae in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 05:19 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM
  5. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 01:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21