# String and Vectors Problem

• 04-14-2007
Matt3000
String and Vectors Problem
I've been having a problem with this code. I don't know why. It's supposed to read a series of numbers (like 12345678 in this example), and depending on what numbers are in the sequence, it outputs code. Why won't this work?

Code:

```string CurrentOutputNums = "12345678"; string outputContents = "";     for(unsigned int c = 0; c < CurrentOutputNums.length(); c++)     {       if(&CurrentOutputNums.substr(c,1) == "1")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 1.0 1.0 1.0 duration))\n )"; continue; }       if(&CurrentOutputNums.substr(c,1) == "2")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 2.0 2.0 2.0 duration))\n )"; continue; }       if(&CurrentOutputNums.substr(c,1) == "3")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 3.0 3.0 3.0 duration))\n )"; continue; }       if(&CurrentOutputNums.substr(c,1) == "4")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 4.0 4.0 4.0 duration))\n )"; continue; }       if(&CurrentOutputNums.substr(c,1) == "5")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 5.0 5.0 5.0 duration))\n )"; continue; }       if(&CurrentOutputNums.substr(c,1) == "6")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 6.0 6.0 6.0 duration))\n )"; continue; }       if(&CurrentOutputNums.substr(c,1) == "7")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 7.0 7.0 7.0 duration))\n )"; continue; }       if(&CurrentOutputNums.substr(c,1) == "8")       { outputContents += "\n\n (cue\n  (mult (sine pitch duration) (env 0.0 0.0 0.0 8.0 8.0 8.0 duration))\n )"; continue; }     }```
This is just a fragment of the code. This part is the only part that doesn't work. It outputs the outputContents string to a file later in the code.
• 04-14-2007
Dave_Sinkula
Code:

`if(&CurrentOutputNums.substr(c,1) == "1")`
Why the &?
• 04-14-2007
Matt3000
I get an error if I don't put that. :-/ I'm using Borland C Compiler 5.5 on Windows XP Service Pack 2. The error says "cannot convert char to char *".
• 04-14-2007
Dave_Sinkula
Quote:

Originally Posted by Matt3000
I get an error if I don't put that. :-/ I'm using Borland C Compiler 5.5 on Windows XP Service Pack 2. The error says "cannot convert char to char *".

Really? I only get errors if I have it there with gcc, and without them, I believe it works as intended.
• 04-14-2007
Matt3000
Really? I have MinGW, I'm going to go try it with that.
• 04-14-2007
Matt3000
Now I get an error that says ISO C++ forbids comparison between pointer an integer.
• 04-14-2007
Oldman47
Does the below compile for you?

Code:

```#include <iostream> #include <string> std::string CurrentOutputNums = "12345678"; std::string outputContents = ""; int main()  {     for(int c = 0; c < CurrentOutputNums.length(); c++)     {           if(CurrentOutputNums.substr(c,1)=="4")       {         outputContents=CurrentOutputNums.substr(c,1);         std::cout<<outputContents<<std::endl;         }                   else{             std::cout<<"nada"<<std::endl;             }           }       std::cin.get();       return 0;       }```
• 04-14-2007
Matt3000
I have to put "using namespace std;" at the top, or else it tells me that "cin" is undeclared.
• 04-14-2007
Oldman47
Actually, I should have typed it as; std::cin.get() My bad -

If that compiles and runs, I'd imagine you should look at your outputContents string.
• 04-14-2007
Matt3000
LOL! It compiles if I use the .substr. I'm checking to see if it works.

And it works! Hooray! Thank you soooooooo much!