-
set manipulation 0
As part of my assignment I have to create an array of ints based on user input. lets say that the user types in the following command:
I have to make an array with those ints in it and of precisely that size. The way my program is set up is that I input the comman word into a string, compare it, and then depending on what it is do further things (look at code below). After that I have to parse the next input taking out the ints seperated by whitespace...I think you know what I mean.
I really don't want to use strtok(), but it looks like I will have to, any other ideas?
Code:
void getCommand()
{
int firstVal = 0; //these will hold the values following each command
int secondVal = 0; //except create, setids, and quit
Sets object;
string command; //holds the first word of the user inputed command
string set;
cout << "> ";
cin >> command;
//create a set
if( command == "create" )
{
cout << endl << command;
//problem lies here
}
//destroy set
else if( command == "destroy" )
{
cin >> firstVal;
object.set( firstVal );
}
........... and so on for other set manipulating functions
thanks
-
You could get the entire line into a string, then construct an istringstream object to use.
Code:
#include <iostream>
#include <sstream>
#include <strstream>
using namespace std;
int main()
{
istringstream in("1 2 3 4 5 6 7 8 9");
int i;
while(in.good())
{
in >> i;
cout << i << " ";
}//while
return 0;
}//main
gg
-
thanks codeplug, I'm not familiar with those libraries but I'll try looking them up because your solution looks much nicer then mine.... I ended up using strtok() for now until I think of something else, and I also ended up writting partially in C....why is it that some stuff is so much easier in C????? anyways here is what I did:
Code:
void getCommand()
{
int firstVal = 0; //these will hold the values following each command
int secondVal = 0; //except create, setids, and quit
int x, i=0; //simple counter
char buff[256]; //buffer for creation of a set
char *p;
Sets object;
string command; //holds the first word of the user inputed command
string set;
cout << "> ";
cin >> command;
//create a set
if( command == "create" )
{
cout << endl << command;
gets( buff );
p = strtok( buff, " " );
while( p != NULL )
{
if( sscanf( p, "%d", &x ) )
cout << p;
else
printf( "error on token %d: integer expected" );
i++;
p = strtok( NULL, " " );
}
}
any other suggestions are more then welcome!
edit:: the above code is partially made for debugging; the actual int is sent to an array in the program
-
codeplug I've run your code, and you either misuderstood what I ment or there is something wrong with the code itself.
When I (user) input any set of ints, it still prints out 1,2,.....9
can you explain the consept of your code for me a bit?
-
Another way
Code:
#include <iostream>
#include <vector>
int main(void)
{
std::vector<int> myArray;
std::string command;
int i;
std::cin >> command;
std::cout <<"Running command: " <<command <<std::endl;
while (std::cin >> i && i != -1)
{
myArray.push_back(i);
}
for (std::vector<int>::iterator it = myArray.begin(); it != myArray.end(); ++it )
{
std::cout <<*it <<" ";
}
std::cout <<std::endl;
return(0);
}
/*
My input/output:
blahblah 1 2 3 4 5 -1
Running command: blahblah
1 2 3 4 5
*/
With the stringstreams, you have to read the input in a string first, then pass that string to a stringstream. This might help (loosely!)
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
-
Thanks Hammer, I've also seldom worked with vectors but they are much more familiar to me then what codeplug showed. Anyways, is there a way of setting the limit to the while loop to be a return (newline) rather then a '-1'?