User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,975 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,650 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1148 | Replies: 42 | Solved
Reply
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,630
Reputation: niek_e is just really nice niek_e is just really nice niek_e is just really nice niek_e is just really nice 
Rep Power: 8
Solved Threads: 168
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Arrays

  #11  
Jul 18th, 2008
hmmm.. That's not quite what I meant
How about something like:
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. vector <int> all_numbers;
  11. stringstream ss;
  12. string input = "1,2,3,4,5";
  13. string buffer;
  14. int integer;
  15. ss << input;
  16. while (getline(ss,buffer,','))
  17. {
  18. //convert buffer to int and put in 'integer'
  19. istringstream strin(buffer);
  20. strin >> integer;
  21. // integer now contains the numeric value of the buffer
  22. //so now push it in the vector
  23. all_numbers.push_back(integer);
  24. }
  25. cout << "Pushed " << all_numbers.size() << " numbers in the vector";
  26. cin.get();
  27. return 0;
  28. }

output: Pushed 5 numbers in the vector

When this code is finished, you will have a vector containing 5 integers. But you can add as much (..) numbers to the string and the vector will dynamically expand.

You can also use the GiveMeNumber() and Show() functions I posted in previous posts on this program.

This code might be a bit much for you, but if you have any question, just ask and I'll try to answer them.

Originally Posted by OmniX View Post
Ok few errors, ill tackle one at a time:
- Unable to open file <sstream>


You're using the (ancient) borland compiler right? Perhaps changing it to #include <sstream.h> will work. But I'm not even sure if Borland already supported stringstreams.... I strongly suggest that you upgrade to a modern IDE+compiler as suggested in one of the other thread
Last edited by niek_e : Jul 18th, 2008 at 7:35 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #12  
Jul 20th, 2008
Ok I have just downloaded and installed "Visual C++ 2008 Express".

Now it is giving me an error about the goto statement label. "xxxxx:" displaying "error C2143: syntax error : missing ';' before '}'".

Should I just try another compiler or ????

The code was working in Borland so I dont see why when I copy and paste it into VC++ that it should not work

Thanks, Regards X
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Jan 2008
Posts: 1,501
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 7
Solved Threads: 189
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: Arrays

  #13  
Jul 20th, 2008
Originally Posted by OmniX View Post
Ok I have just downloaded and installed "Visual C++ 2008 Express".

Now it is giving me an error about the goto statement label. "xxxxx:" displaying "error C2143: syntax error : missing ';' before '}'".

Should I just try another compiler or ????

The code was working in Borland so I dont see why when I copy and paste it into VC++ that it should not work

Thanks, Regards X



Visual Studio 2008 Express should be able to handle niek's code. I can't imagine why it wouldn't. I copied and pasted niek's code into Dev C++ and it compiled and ran fine, and I did the same with Visual Studio 2008 (non-Express edition) and it did fine there too. I don't see any goto statements in niek's code. You started a console project, right? Try copying and pasting niek's code above into Visual Studio 2008 again and seeing if it compiles and runs. It should, I think. The error you list sounds like a typo somewhere, maybe?
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #14  
Jul 21st, 2008
Nothing wrong with niek_e code, its the one im working on.

I have changed compilers to codeblocks and its working no, so problem solved.

Onto the next one.

Im not sure If I have current brain freeze of trying to get my head around the new programming concept of C++ but from niek_e previous example I am trying to do exactly what he did BUT instead of just one variable I trying to break up the whole string into individual variables then compare them and then reattach all the variables as a string and add the "whole" variable to the string array.

Are you able to compare string vs. string in C++ or string vs. int?

Thanks, Regards X

PS: I apologize again about the newbieness
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #15  
Jul 21st, 2008
I was revising my code, it may have to do with my vectors being a "string"???
Last edited by OmniX : Jul 21st, 2008 at 1:44 am.
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,630
Reputation: niek_e is just really nice niek_e is just really nice niek_e is just really nice niek_e is just really nice 
Rep Power: 8
Solved Threads: 168
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Arrays

  #16  
Jul 21st, 2008
I'm not sure I understand what you're asking. Could you post your current code and the errors it gives you?
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #17  
Jul 21st, 2008
  1. //int c_same = 0;
  2.  
  3. //strings
  4. string c_array_temp;
  5. string c_string;
  6. string c_temp;
  7. stringstream c_stringstream;
  8.  
  9. //vectors
  10. vector<string> c_array;
  11.  
  12. // Checks if 3 numbers are the same
  13. for (int numS= 0; numS< combinations_array.size(); numS++) {
  14. c_stringstream << c_array[numS];
  15. for (int numB= 0; numB< combinations_array.size(); numB++) {
  16. getline(c_stringstream, c_temp,' ');
  17. c_array_temp >> c_temp;
  18. c_same = 0;
  19. // One
  20. if (one == c_array_temp ||
  21. ) {
  22. c_same++;
  23. }
  24. // Two
  25. if (two == c_array_temp) {
  26. c_same++;
  27. }
  28. // Three
  29. if (three == c_array_temp) {
  30. c_same++;
  31. }
  32. // Four
  33. if (four == c_array_temp) {
  34. c_same++;
  35. }
  36. // Five
  37. if (five == c_array_temp) {
  38. c_sameme++;
  39. }
  40. // Six
  41. if (six == c_array_temp) {
  42. c_same++;
  43. }
  44. if (c_same > 3) {
  45. goto skip;
  46. }
  47. }
  48.  
  49. // Writes the numbers to the string
  50. for (int numW=0; numW<6; numW++) {
  51. switch(numW) {
  52. case 0:
  53. c_string.append(one);
  54. c_string.append(" ");
  55. break;
  56. case 1:
  57. c_string.append(two);
  58. c_string.append(" ");
  59. break;
  60. case 2:
  61. c_string.append(three);
  62. c_string.append(" ");
  63. break;
  64. case 3:
  65. c_string.append(four);
  66. c_string.append(" ");
  67. break;
  68. case 4:
  69. c_string.append(five);
  70. c_string.append(" ");
  71. break;
  72. case 5:
  73. c_string.append(six);
  74. break;
  75. }
  76. }
  77.  
  78. // Writes the numbers to the array
  79. c_array.push_back(c_string);

error: no match for 'operator<<' in c_array_temp << c_temp'|
error: no match for 'operator==' in 'one == c_array_temp'| [happens for all one - six]
error: no match for 'operator>>' in c_string >> one'|

Sorry I have tried my best to construct something but I have no luck

Hope you can help, Thanks Regards X
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,630
Reputation: niek_e is just really nice niek_e is just really nice niek_e is just really nice niek_e is just really nice 
Rep Power: 8
Solved Threads: 168
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Arrays

  #18  
Jul 21st, 2008
Without looking trough all of your code, there are a few syntax errors, for example:
  1. if (one == c_array_temp ||
  2. ) {
Also, the variables "one", "two", "three" etc are never declared....

I think you're making it to hard on yourself. If you want to break the string into integers and store them all, how about using the code I posted earlier?
You can access all the numbers in the array in the same way you would do it with an array:
cout << all_numbers[0] ;
would give the first number stored in the vector. If you understand how you can access the vector, the only thing you would have to do is to write a "compare-algorithm"
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #19  
Jul 21st, 2008
niek_e thanks for the advice.

Its the early the hours of the morning and been trying to fix this the whole day.

I think I will sleep on it and take your advice and try something tomorrow.

Thanks, Regards X

PS: Ill keep you posted
"You never stop learning" - OmniX
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Arrays

  #20  
Jul 22nd, 2008
Ok i have decided to start again and break up my problem into little problems and solves them one by one and eventually and hopefully it is complete working order!!!

Ok First Problem:

How do I get 6 variables and insert them into a string (seperated by spaces " ") and then it into an array?

Like This?

  1. int a = 1;
  2. int b = 2;
  3. int c = 3;
  4. int d = 4;
  5. int e = 5;
  6. int f = 6;
  7.  
  8. string abc;
  9.  
  10. vector<string> abcd;
  11.  
  12. abc << a << " " << b << " " << c << " " <<
  13. d << " " << e << " " << f;
  14.  
  15. abcd.append(abc);

Now would the above code do what I require?

Thankyou, Regards X

PS: I am trying my best!!!
"You never stop learning" - OmniX
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:00 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC