?
Solved

How to input values in Multidimensional array

Posted on 2008-02-04
3
Medium Priority
?
542 Views
Last Modified: 2010-04-01
what is the best way to input values into a multidimensional array.  Below is part of my coding for a multidimensional array with a way to input values.

I"m running into a problem when the program is run and the user goes to input values...the inputs aren't lining up with the proper columns... The user has to input the first value and space over twice to input the next value and so .. how can I Have it automatically line up to each corrsponding columns?

The coding isn't my final coding i'm just doing it in parts to get each piece to work before writing the actual program and cleaning it up.

Thanks for the help

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{
   
                             
   
    int rooms[20] = {1,2,3,4,5,6,7,8,9,10,
                     11,12,13,14,15,16,17,
                     18,19,20};
                     
    const char* floors[16] = {"Floor 1","Floor 2","Floor 3","Floor 4","Floor 5",
                              "Floor 6","Floor 7","Floor 8","Floor 9","Floor 10",
                              "Floor 11","Floor 12","Floor 13","Floor 14",
                              "Floor 15","Floor 16"};
                             
    char hotel_status[16][20];
   
   
    cout <<"Enter Rooms status for each room" <<endl;
    cout <<endl;
    cout <<"Room Status Grid" <<endl;
    cout <<endl;
    cout <<"Room Available - ready for guest   = 'A'" <<endl;
    cout <<"Room Available - needs cleaning    = 'a'" <<endl;
    cout <<"Room Not Available - no cleaning   = 'N'" <<endl;
    cout <<"Room Not Available - need cleaning = 'n'" <<endl;
    cout <<endl;
     
   cout << endl;
   cout << setw(40) <<"ROOMS" <<endl;
   cout <<endl;
   cout << left <<"          ";
   
   for(int i = 0; i < 20; i++)
   {
           
          cout << setw(3) << rooms[i];
         
   }
   cout << endl;
               
             for(int r = 0; r < 16; r++)
             {
                     cout << endl;
                     cout << left << setw(10) << floors[r];
           
                     for(int c = 0; c < 20; c++)
                     {                                                  
                             cin >> hotel_status[r][c];
                           
                     }
             }
         
   cout << endl;
   
   
//-----------------------------------------------------------------------------  
       
   cout << endl;
   cout << setw(40) <<"ROOMS" <<endl;
   cout <<endl;
   cout << left <<"          ";
   
   for(int i = 0; i < 20; i++)
   {
           
          cout << setw(3) << rooms[i];
         
   }
   cout << endl;
               
             for(int r = 0; r < 16; r++)
             {
                     cout << endl;
                     cout <<left <<setw(10) << floors[r];
           
                     for(int c = 0; c < 20; c++)
                     {                          
                             cout << setw(3) << hotel_status[r][c];
                     }
             }
         
   cout << endl;
           
system("pause");
return 0;
}
0
Comment
Question by:WestonGroup
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20817720
In a continuous row will be difficult, but the below code will keep the columns consistent:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
 
using namespace std;
 
int main()
{
   
                             
   
    int rooms[20] = {1,2,3,4,5,6,7,8,9,10,
                     11,12,13,14,15,16,17,
                     18,19,20};
                     
    const char* floors[16] = {"Floor 1","Floor 2","Floor 3","Floor 4","Floor 5",
                              "Floor 6","Floor 7","Floor 8","Floor 9","Floor 10",
                              "Floor 11","Floor 12","Floor 13","Floor 14",
                              "Floor 15","Floor 16"};
                             
    char hotel_status[16][20];
   
   
    cout <<"Enter Rooms status for each room" <<endl;
    cout <<endl;
    cout <<"Room Status Grid" <<endl;
    cout <<endl;
    cout <<"Room Available - ready for guest   = 'A'" <<endl;
    cout <<"Room Available - needs cleaning    = 'a'" <<endl;
    cout <<"Room Not Available - no cleaning   = 'N'" <<endl;
    cout <<"Room Not Available - need cleaning = 'n'" <<endl;
    cout <<endl;
     
   cout << endl;
   cout << setw(40) <<"ROOMS" <<endl;
   cout <<endl;
   cout << left <<"          ";
   
   for(int i = 0; i < 20; i++)
   {
           
          cout << setw(3) << rooms[i];
         
   }
   cout << endl;
               
             for(int r = 0; r < 16; r++)
             {
                     string adjust = "          ";
                     cout << endl;
                     cout << left << setw(10) << floors[r] << endl;
           
                     for(int c = 0; c < 20; c++)
                     {                          
                             cout << adjust;  // <--- reposistion!                      
                             cin >> hotel_status[r][c];
                             adjust += "   "; // move to the next one
                     }
             }
         
   cout << endl;
   
   
//-----------------------------------------------------------------------------  
       
   cout << endl;
   cout << setw(40) <<"ROOMS" <<endl;
   cout <<endl;
   cout << left <<"          ";
   
   for(int i = 0; i < 20; i++)
   {
           
          cout << setw(3) << rooms[i];
         
   }
   cout << endl;
               
             for(int r = 0; r < 16; r++)
             {
                     cout << endl;
                     cout <<left <<setw(10) << floors[r];
           
                     for(int c = 0; c < 20; c++)
                     {                          
                             cout << setw(3) << hotel_status[r][c];
                     }
             }
         
   cout << endl;
           
system("pause");
return 0;
}

Open in new window

0
 

Author Comment

by:WestonGroup
ID: 20817865
How would I input a single room value...  I can figure out how to input all values..but how do I do just a single...so how could I reference 1 room and change that value..

So if all values were say   0   how can I change the value in hotel in floor 5 room 5  which is (4,4) ?

thats where i'm running into problems

how could the user be prompted to select that room to change...
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20817912
>>how could the user be prompted to select that room to change...

That's relatively simple, just think of it as inputting coordinates, e.g.
int room, floor;
 
cout << "Which place do you want to change?" << endl;
cout << "Floor: ";
cin >> floor;
cout << "Room: ";
cin >> room;
cout "Status: ";
cin >> hotel_status[floor][room];

Open in new window

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question