The example is close to perfect for the logic I require BUT it skips the step of assigning individual variables to 1 2 3 4 5 instead its just parts of an array. Everything else works beautifuly.

2 Questions:
- For the for loop why is it "unsigned int" and not just "int"?
- Why x11? You said to make it an int? but just makes the out put 11 22 33 44 55?

Thanks Again nearly there :(, Regards X

The example is close to perfect for the logic I require BUT it skips the step of assigning individual variables to 1 2 3 4 5 instead its just parts of an array. Everything else works beautifuly.

As I mentioned before: I would really recommend that you use a vector/array or else you won't be able to use a loop. You would get something like:

``````string str = "1,2,3,4,5";
get one var;
convert to int;
store in var a;

get one var;
convert to int;
store in var b;

get one var;
convert to int;
store in var c;``````

As you can see, this would result in some real ugly code...

- For the for loop why is it "unsigned int" and not just "int"?

`vector.size()` returns an unsigned int, so in the loop I have to compare `unsigned int` with `unsigned int` :)

- Why x11? You said to make it an int? but just makes the out put 11 22 33 44 55?

It was just to show that the string was really converted to an int and that you can use the +/-* operators on it. It was just to give an example and is of no use for the working of the program, so you can delete it

I will try work something out with the "ugly code" and get back to you.

Thanks Again for your continued help.

Regards, X

When I tried to implement them for my problem, all the code and logic worked minus one (below).

``````while (getline(ss,buffer,','))
{
//convert buffer to int and put in 'integer'
istringstream strin(buffer);
strin >> integer;
// integer now contains the numeric value of the buffer
//so now push it in the vector
all_numbers.push_back(integer);
}``````

If I try using that while loop more than once, its useless as it is bypassed and keeps using the old values stored in the variables and dosent take in the seperate the new values.

I have been doing some research is it better to use a tokenizer to solve my problem instead?

Thanks again, nearly there!!!!

Regards, X

If I try using that while loop more than once, its useless as it is bypassed and keeps using the old values stored in the variables and dosent take in the seperate the new values.

Hmm .. I'm not quite sure what the actual problem is, but maybe the following helps to understand things (It's a modified version of what niek_e already posted)

``````#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
// 3 strings as input
string inputs[3]  = {{"1,2,3"}, {"4,5,6"}, {"7,8,9"}};
string buffer;
int integer;

// iterate through the 3 input strings ...
for(int ii = 0; ii < 3; ++ii)
{
stringstream ss;
// store the input string in the stringstream
ss << inputs[ii];

// extract and cout the numbers one at the time ...
while (getline(ss,buffer,','))
{
// convert to an int
istringstream strin(buffer);
strin >> integer;

cout << integer;
}
}

cin.get();
return 0;
}``````

Output is ...

``123456789``

quick Q before I try go implement it myself:

Why is the stringstream initialized within the forloop instead of outside?

Couldnt compile code:

``````ERROR:
Vector3.cpp||In function `int main()':|
Vector3.cpp|9|error: brace-enclosed initializer used to initialize `std::string'|
Vector3.cpp|9|error: brace-enclosed initializer used to initialize `std::string'|
Vector3.cpp|9|error: brace-enclosed initializer used to initialize `std::string'|
||=== Build finished: 3 errors, 0 warnings ===|``````

Thanks, Regards X

It saves mitrmkar the trouble of deleting the content of the stream. It all has to do with the scope of which vars are declared in.

commented: Without your help throughout the last 2 weeks I would have been lost, thankyou so much :) +1

Why is the stringstream initialized within the forloop instead of outside?

That way you automatically get a 'fresh' stringstream object meaning that you don't have to take care of resetting it in any way.

Couldnt compile code:

``````ERROR:
Vector3.cpp||In function `int main()':|
Vector3.cpp|9|error: brace-enclosed initializer used to initialize `std::string'|
Vector3.cpp|9|error: brace-enclosed initializer used to initialize `std::string'|
Vector3.cpp|9|error: brace-enclosed initializer used to initialize `std::string'|
||=== Build finished: 3 errors, 0 warnings ===|``````

``std::string inputs[3]  = {"1,2,3", "4,5,6", "7,8,9"};``

*(^#\$@#&)\$^@#&*(\$@#^*(&\$&#@*(\$&@#*(\$^#@)*&\$^@#*&\$@#^*&\$#@^\$*&#@^\$#@&\$^#@*&\$^@#&*\$#@^&*\$#@^*(......

Nice Translation I found the problem...

The goto is stuffing up my function due to not being able to be labeled inside the function it is used...

``````for(int i=0; i<10; i++) {
if (i > 0) {
goto jump;
} else {
cout << i;
}
jump:; // THIS IS WHERE I WANT JUMP
}
jump:; // THATS WHERE I HAVE TO USE IT``````

so any ideas how to get around this?

Then I think I am done!!! *crosses fingers*

Thanks for putting up with me :(

Regards, X

PS: The example is a pathetic one it was just used to demonstrate the point

Try not to use the `goto` , have you considered using `break` instead

``````for(int i=0; i<10; i++) {
if (i > 0)
break;    // <- breaks out of the for loop ...
else
cout << i;
}``````
commented: Thanks for the help +1

ya I have BUT my problems are:
- nested if statements
- skip code

I really prefer not to use a bool on/off trigger as I had alot of problems before when I did but I guess its a last resort, if no one can come up with a solution.

I would like to say a special thankyou to everyone who has helped me the last 2 weeks.

A special thankyou to niek_e, as without him I would have been lost before started, thankyou so much.

Takecare and all the best everyone.

Till my next C++ problem!!!

(Lets hope its not too soon :( hahaha)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.