Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have an array grid of 5 x 5. I want the user to user to give an option to input the cells at runtime. For eg: For the first row, the user can enter
* * * _ *
_ means he skipped the fourth place with a space
This would mean
a[0][0]='*'
a[0][1]='*'
a[0][2]='*'
a[0][3]=' '//empty
a[0][4]='*'

I would also need to keep a track of 5 lines of input(as there are five rows in the grid) for the user. How should I go about this? For specific format of input, should sscanf be used?


Secondly, I need to revise the concepts of strings, get, getline, gets, sscanf, etc. I would be thankful for some not-so-hard-to-follow links where I could read about these things.
P.S Merry Christmas!! :-)
Edit :rephrased
Posted
Updated 25-Dec-10 7:47am
v4
Comments
Dr.Walt Fair, PE 25-Dec-10 13:40pm    
It sounds like you need to learn some basic C/C++ if you are having a hard time with those basic concepts! Try Google for some online information or look for a book on learning C/C++.
The_Real_Chubaka 26-Dec-10 9:50am    
There are a lot of tutorials, notes... on Google. I've just googled 'gets c++' and i got more than 6 million results. Most of the links have example codes that are very easy to understand. I wonder what Google you are using :)

google string tokenizer

Note that tokens should be std::vector<std::string xmlns:std="#unknown"> & below, but codeproject changed the code for some reason.

//The tokenize function is borrowed form here:
//http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-7.html
//It is used to parse the input messages
void tokenize(const std::string & str,
	      std::vector<string> & tokens,
	      std::string const & delimiters) {
	tokens.resize(0);
    // Skip delimiters at beginning.
	std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
	while (std::string::npos != pos || std::string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
};
 
Share this answer
 
v3
Comments
Dalek Dave 8-Jan-11 8:27am    
Good Answer, 5.
You could try something like this:

#include <stdio.h>

void main(void)
{
	char a[5][5];

	// Read data
	for(int i=0;i<5;i++){			// Row
		for(int j=0;j<5;j++)		// Column
			a[i][j] = getchar();	// Read the character
		getchar();			// Read the enter-key
	}
}


This program will fill your array a[][] with the characters entered like so:
*****<enter>
*****<enter>
*****<enter>
*****<enter>
*****<enter>


Note that the input is assumed to be in the correct format, and there is no feedback yet if anything goes wrong. To check if an entered character is valid, you could add a check like this:
// Read data
for(int i=0;i<5;i++){            // Row
    for(int j=0;j<5;j++){        // Column
        a[i][j] = getchar();    // Read the character
        if(a[i][j] != ' ' && a[i][j] != '*'){
                    // Error handling code goes here
        }
    }
    getchar();          // Read the enter-key
}
 
Share this answer
 
v2
Comments
Dalek Dave 8-Jan-11 8:27am    
Good Call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900