By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

cin, cout rows and columns

P: 10
I don't know how do get the rows can you help me please :(

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. int rows;
  7. int cols;
  8. string sym;
  9. int count;
  10.  
  11. cout << "Please enter a symbol to be display: ";
  12. cin >> sym;
  13.  
  14. do
  15. {
  16. cout << "\tPlease enter how many colunms to create: ";
  17. cin >> cols;
  18. cout << "\n\n\n";
  19. }
  20.  
  21. while(cols < 10);
  22.  
  23. for (count = 1; count <= cols; count++)
  24.  
  25. cout << "\n\t\t" << sym;
  26.  
  27. cin.ignore();
  28. return 0;
  29. }
Jun 3 '07 #1
Share this Question
Share on Google+
10 Replies


Savage
Expert 100+
P: 1,764
By using nested loops,do u know what nested loops are?

Savage
Jun 3 '07 #2

P: 10
By using nested loops,do u know what nested loops are?

Savage
nested loop is a loop that inside another loop?, :( I'm so behind on this I can't figure out how to do this anymore and the program is due this Monday, my friend told me to do it as

Expand|Select|Wrap|Line Numbers
  1. for (int x=0; x<rows; x++)
  2. {
  3.   for (int y=0; y<cols; y++)
  4.   {
  5.     cout >> "enter character: ";
  6.     cin << sym;
  7.  
  8.   }
  9. }
but I just don't understand why he prompt user to input character after rows and column and how would it generate in to a map? I'm very confuse and so afraid to ask him again because he might get mad this time after so many question already ask lol.
Jun 3 '07 #3

Savage
Expert 100+
P: 1,764
Expand|Select|Wrap|Line Numbers
  1. for (int x=0; x<rows; x++)
  2. {
  3.   for (int y=0; y<cols; y++)
  4.   {
  5.     cout >> "enter character: ";
  6.     cin << sym;
  7.  
  8.   }
  9. }
Tell me something:

Are u suppossed to print one character,or more of them?

and please use codetags when posting ur code ;)
Savage
Jun 3 '07 #4

P: 10
I'm suppose to create a floor plan of a room that contain a floor tiles, wall and entry door and a escape hatch, a key and a piranha and have at less 10 rows and 10 columns by prompting user to enter any symbol they desire, I just don't know what method I should use :(
Jun 3 '07 #5

Savage
Expert 100+
P: 1,764
I'm suppose to create a floor plan of a room that contain a floor tiles, wall and entry door and a escape hatch, a key and a piranha and have at less 10 rows and 10 columns by prompting user to enter any symbol they desire, I just don't know what method I should use :(
Do u know how it's supposed to look,were there a example in ur assigment?

Please be more specific and supply more information

Savage
Jun 3 '07 #6

P: 10
Do u know how it's supposed to look,were there a example in ur assigment?

Please be more specific and supply more information

Savage

example

please enter a character for wall
cin >> w
please enter how many rows:
cin >> 10;
please enter how many columns:
cin >> 10;



so it create a map with 10 rows and columns

wwwwwwwwww
w w
w w
w w
w w
w w
w w
w w
w w
wwwwwwwwww
Jun 3 '07 #7

Savage
Expert 100+
P: 1,764
example

please enter a character for wall
cin >> w
please enter how many rows:
cin >> 10;
please enter how many columns:
cin >> 10;



so it create a map with 10 rows and columns

wwwwwwwwww
w w
w w
w w
w w
w w
w w
w w
w w
wwwwwwwwww
Ok here are some hints:

1.ask user to input a character.
2.create nested loops,so that counter from first loop tells u in which row u are and secound one is printing characters in row.
3.in nested loop check the counter from the first loop,if that value equals to 1 or to number of rows,print just symbol if not check the counter from the inner loop,if it has a value of 1 or of characters per row,print symbol else print whitespace.
4.close parenthisis for the inner loop,and add endl,or '\n' to move on to next row.

Savage
Jun 3 '07 #8

P: 10
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     //string sym;
  8.     int x, y;
  9.     int rows;
  10.     int cols;
  11.     do
  12.     {
  13.     cout << "Please enter how many rows: ";
  14.     cin >> rows;
  15.     } while(rows <10);
  16.  
  17.     do
  18.     {
  19.         cout << "Please enter how many column: ";
  20.         cin >> cols;
  21.     } while (cols <10);
  22. for(int y =1; y <= rows; y++)
  23.  
  24.     cout << y << endl;
  25. for(int x =1; x <= cols; x++)
  26.     cout << x;
  27.  
  28. cin.ignore(); cin.ignore();
  29.   return 0;
  30. }
  31.  
okay this is all I have, now how do I prompt user to input for chracter to create row and column and an entrance also exit door. sorry I can't figure it out anymore :(
Jun 4 '07 #9

P: 10
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9.     int rows;
  10.     int cols;
  11.     char wall;
  12.     do
  13.     {
  14.     cout << "Please enter how many rows: ";
  15.     cin >> rows;
  16.     } while(rows <10);
  17.      string map[rows];
  18.     do
  19.     {
  20.         cout << "Please enter how many column: ";
  21.         cin >> cols;
  22.     } while (cols <10);
  23.     cout << "Please enter a rows for wall: ";
  24.     cin >> wall;
  25. for(int i=1; i< rows; i++)
  26. {   
  27. do
  28. {
  29.  
  30.     map[i] = string(getline()); 
  31. } while (map[i].length!=cols);
  32.  
  33. for (int i = 0; i < rows; i++)
  34.    for (int j = 0; j < cols; j++)
  35.         if (map[i][j] == wall) map[i][j] = ' ';
  36.  
  37.  
  38. }
  39. // for(int x =1; x <= cols; x++)
  40.    // cout << x;
  41.  
  42. cin.ignore(); cin.ignore();
  43.   return 0;
  44. }
  45.  

this is what I have so far but is keep giving me an error because of map size any idea why?
Jun 4 '07 #10

Savage
Expert 100+
P: 1,764
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9.     int rows;
  10.     int cols;
  11.     char wall;
  12.     do
  13.     {
  14.     cout << "Please enter how many rows: ";
  15.     cin >> rows;
  16.     } while(rows <10);
  17.      string map[rows];
  18.     do
  19.     {
  20.         cout << "Please enter how many column: ";
  21.         cin >> cols;
  22.     } while (cols <10);
  23.     cout << "Please enter a rows for wall: ";
  24.     cin >> wall;
  25. for(int i=1; i< rows; i++)
  26. {   
  27. do
  28. {
  29.  
  30.     map[i] = string(getline()); 
  31. } while (map[i].length!=cols);
  32.  
  33. for (int i = 0; i < rows; i++)
  34.    for (int j = 0; j < cols; j++)
  35.         if (map[i][j] == wall) map[i][j] = ' ';
  36.  
  37.  
  38. }
  39. // for(int x =1; x <= cols; x++)
  40.    // cout << x;
  41.  
  42. cin.ignore(); cin.ignore();
  43.   return 0;
  44. }
  45.  

this is what I have so far but is keep giving me an error because of map size any idea why?
U are building array of strings and putting it on the stack:

string map[rows];

this is not correct,because rows is variable and stack can only support constant values,u need to allocate it dynamicly.

In c++ that is done with using new:

string *map/a pointer to a string called map

map=new map[rows] //allocating memory for map

now it can be used same way:

map[i],...

There are also more of the problems but we will diel with them later,first make sure to allocate memory for string map

Savage
Jun 4 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.