cin, cout rows and columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oi123
    New Member
    • Apr 2007
    • 10

    cin, cout rows and columns

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

    [code=cpp]#include<iostre am>
    #include<string >

    using namespace std;

    int rows;
    int cols;
    string sym;
    int count;

    cout << "Please enter a symbol to be display: ";
    cin >> sym;

    do
    {
    cout << "\tPlease enter how many colunms to create: ";
    cin >> cols;
    cout << "\n\n\n";
    }

    while(cols < 10);

    for (count = 1; count <= cols; count++)

    cout << "\n\t\t" << sym;

    cin.ignore();
    return 0;
    }[/code]
    Last edited by AdrianH; Jun 3 '07, 09:59 PM. Reason: Please use [code=cpp][/code] tags to improve readability. AND INDENT PEOPLE!!!
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    By using nested loops,do u know what nested loops are?

    Savage

    Comment

    • oi123
      New Member
      • Apr 2007
      • 10

      #3
      Originally posted by Savage
      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

      [code=cpp]for (int x=0; x<rows; x++)
      {
      for (int y=0; y<cols; y++)
      {
      cout >> "enter character: ";
      cin << sym;

      }
      }[/code]

      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.
      Last edited by AdrianH; Jun 3 '07, 10:00 PM. Reason: Please use [code=cpp][/code] tags to improve readability

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        [CODE=cpp]for (int x=0; x<rows; x++)
        {
        for (int y=0; y<cols; y++)
        {
        cout >> "enter character: ";
        cin << sym;

        }
        }[/CODE]

        Tell me something:

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

        and please use codetags when posting ur code ;)
        Savage

        Comment

        • oi123
          New Member
          • Apr 2007
          • 10

          #5
          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 :(

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by oi123
            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

            Comment

            • oi123
              New Member
              • Apr 2007
              • 10

              #7
              Originally posted by Savage
              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

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by oi123
                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

                Comment

                • oi123
                  New Member
                  • Apr 2007
                  • 10

                  #9
                  Code:
                  #include <iostream>
                  #include<string>
                  using namespace std;
                  
                  int main()
                  {
                  	//string sym;
                  	int x, y;
                  	int rows;
                  	int cols;
                  	do
                  	{
                  	cout << "Please enter how many rows: ";
                  	cin >> rows;
                  	} while(rows <10);
                  	
                  	do
                  	{
                  		cout << "Please enter how many column: ";
                  		cin >> cols;
                  	} while (cols <10);
                  for(int y =1; y <= rows; y++)
                  
                  	cout << y << endl;
                  for(int x =1; x <= cols; x++)
                  	cout << x;
                  
                  cin.ignore(); cin.ignore();
                    return 0;
                  }
                  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 :(

                  Comment

                  • oi123
                    New Member
                    • Apr 2007
                    • 10

                    #10
                    Code:
                    #include <iostream>
                    #include<string>
                    using namespace std;
                     
                    
                    int main()
                    {
                      
                        int rows;
                        int cols;
                    	char wall;
                        do
                        {
                        cout << "Please enter how many rows: ";
                        cin >> rows;
                        } while(rows <10);
                         string map[rows];
                        do
                        {
                            cout << "Please enter how many column: ";
                            cin >> cols;
                        } while (cols <10);
                    	cout << "Please enter a rows for wall: ";
                    	cin >> wall;
                    for(int i=1; i< rows; i++)
                    {   
                    do
                    {
                    	
                    	map[i] = string(getline()); 
                    } while (map[i].length!=cols);
                    
                    for (int i = 0; i < rows; i++)
                       for (int j = 0; j < cols; j++)
                            if (map[i][j] == wall) map[i][j] = ' ';
                    
                    
                    }
                    // for(int x =1; x <= cols; x++)
                       // cout << x;
                     
                    cin.ignore(); cin.ignore();
                      return 0;
                    }

                    this is what I have so far but is keep giving me an error because of map size any idea why?

                    Comment

                    • Savage
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1759

                      #11
                      Originally posted by oi123
                      Code:
                      #include <iostream>
                      #include<string>
                      using namespace std;
                       
                      
                      int main()
                      {
                        
                          int rows;
                          int cols;
                      	char wall;
                          do
                          {
                          cout << "Please enter how many rows: ";
                          cin >> rows;
                          } while(rows <10);
                           string map[rows];
                          do
                          {
                              cout << "Please enter how many column: ";
                              cin >> cols;
                          } while (cols <10);
                      	cout << "Please enter a rows for wall: ";
                      	cin >> wall;
                      for(int i=1; i< rows; i++)
                      {   
                      do
                      {
                      	
                      	map[i] = string(getline()); 
                      } while (map[i].length!=cols);
                      
                      for (int i = 0; i < rows; i++)
                         for (int j = 0; j < cols; j++)
                              if (map[i][j] == wall) map[i][j] = ' ';
                      
                      
                      }
                      // for(int x =1; x <= cols; x++)
                         // cout << x;
                       
                      cin.ignore(); cin.ignore();
                        return 0;
                      }

                      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

                      Comment

                      Working...