Thread: String and Vectors Problem

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    47

    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.
    Windows XP Service Pack 2
    Borland C++ Compiler Version 5.5

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    if(&CurrentOutputNums.substr(c,1) == "1")
    Why the &?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    47
    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 *".
    Windows XP Service Pack 2
    Borland C++ Compiler Version 5.5

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Matt3000 View Post
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    47
    Really? I have MinGW, I'm going to go try it with that.
    Windows XP Service Pack 2
    Borland C++ Compiler Version 5.5

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    47
    Now I get an error that says ISO C++ forbids comparison between pointer an integer.
    Windows XP Service Pack 2
    Borland C++ Compiler Version 5.5

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    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;
          }
    Last edited by Oldman47; 04-14-2007 at 08:30 PM.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    47
    I have to put "using namespace std;" at the top, or else it tells me that "cin" is undeclared.
    Windows XP Service Pack 2
    Borland C++ Compiler Version 5.5

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    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.
    Last edited by Oldman47; 04-14-2007 at 08:32 PM.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    47
    LOL! It compiles if I use the .substr. I'm checking to see if it works.

    And it works! Hooray! Thank you soooooooo much!
    Windows XP Service Pack 2
    Borland C++ Compiler Version 5.5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some questions with strings, vectors, references...
    By samus250 in forum C++ Programming
    Replies: 11
    Last Post: 07-10-2008, 02:31 PM
  2. Using Vectors (cont) - Sort Problem
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2007, 06:31 AM
  3. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  4. Shell Sort with Vectors Problem
    By TheSpoiledMilk in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2005, 03:05 PM
  5. how get string vector from class in file?
    By tord in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2005, 09:58 AM