Problem with part of program

This is a discussion on Problem with part of program within the C++ Programming forums, part of the General Programming Boards category; I cant figure out why the complier is giving me the error: cannot convert ‚std::string‚ to ‚int‚ in return here ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Problem with part of program

    I cant figure out why the complier is giving me the error: cannot convert ‚std::string‚ to ‚int‚ in return


    here is part of my code:

    {
    for (int i= 1; i <= number; i++)
    {
    ones = (number % 10);
    }
    string ones_place(ones);
    cout << number << " = " << ones << endl;
    }
    cin >> number;
    }

    cout << endl;
    return 0;
    }

    string ones_place(int& number)
    {
    string ones;
    if (number == 1)
    ones = "one";
    else if ( number ==2)
    ones = "two";
    else if (number ==3)
    ones = "three";
    else if (number ==4)
    ones = "four";
    else if (number ==5)
    ones = "five";
    else if (number ==6)
    ones = "six";
    else if (number ==7)
    ones = "seven";
    else if (number ==8)
    ones = "eight";
    else if (number == 9)
    ones = "nine";
    else if (number == 10)
    ones = "ten";
    else if (number == 11)
    ones = "eleven";
    else if (number == 12)
    ones = "twelve";
    else if (number == 13)
    ones = "thirteen";
    else if (number == 14)
    ones = "fourteen";
    else if (number == 15)
    ones = "fifteen";

    return ones;
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    I'm actually curious as to how you were allowed to write that post without code tags
    The programmerís wife tells him: ďRun to the store and pick up a loaf of bread. If they have eggs, get a dozen.Ē
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    tried

    i thought i put code tags in- it said CODE before and after my program....

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1. This:
    Code:
    string ones_place(ones);
    Needs to be like this:
    Code:
    oneplace(ones)
    Or if you were trying to forward declare:
    Code:
    string ones_place(int& number);
    Edit: What is ones defined as? An int?
    Last edited by manutd; 11-08-2006 at 07:10 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    fixed a little

    ok well i fixed it a little bit. I am trying to get just the ones place of a number to print out the word for that number. I will work on the other places now. It finally let me compile but did not change anything.

    word is the word i want to come out.

    ones is the ones place from the number after i do modulus. I am then passing ones into the string function. ones is an int but then i need it convert into word and pass pass the actul word. here is my new code.


    Code:
     for (int i= 1; i <= number; i++)
              {
               ones = (number % 10);
              }
            oneplace(ones);
          cout << number << " = " << ones << endl;
          }
      cin >> number;
      }
    
     cout << endl;
     return 0;
    }
    
    string oneplace(int& ones)
    {
      string word;
      if (ones == '1')
        word = "one";
      else if ( ones =='2')
        word = "two";
      else if (ones == '3')
        word = "three";
      else if (ones == '4')
        word = "four";
      else if (ones == '5')
        word = "five";
      else if (ones == '6')
        word = "six";
      else if (ones == '7')
        word = "seven";
      else if (ones == '8')
        word = "eight";
      else if (ones == '9')
        word = "nine";
    
      return word;
    }
    thanks

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by ammochck21
    ok well i fixed it a little bit. I am trying to get just the ones place of a number to print out the word for that number. I will work on the other places now. It finally let me compile but did not change anything.

    word is the word i want to come out.

    ones is the ones place from the number after i do modulus. I am then passing ones into the string function. ones is an int but then i need it convert into word and pass pass the actul word. here is my new code.


    Code:
     for (int i= 1; i <= number; i++)
              {
               ones = (number % 10);
              }
            oneplace(ones);
          cout << number << " = " << ones << endl;
          }
      cin >> number;
      }
    
     cout << endl;
     return 0;
    }
    
    string oneplace(int& ones)
    {
      string word;
      if (ones == '1')
        word = "one";
      else if ( ones =='2')
        word = "two";
      else if (ones == '3')
        word = "three";
      else if (ones == '4')
        word = "four";
      else if (ones == '5')
        word = "five";
      else if (ones == '6')
        word = "six";
      else if (ones == '7')
        word = "seven";
      else if (ones == '8')
        word = "eight";
      else if (ones == '9')
        word = "nine";
    
      return word;
    }
    thanks

    Well I guess the code won't work as you expected because you used apostrophe. With apostrophe, you'll end up comparing the ascii value of the number.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    still problem

    Ok well even when I take the apostrophes out the result shows this:

    2 =
    5 =
    -5 = is not a valid number for translation.
    6 =
    3 =
    1500000 = is not a valid number for translation.


    So its printing out the ones place of the number...but i still cant get it to pass the word back and print it. I dont know why. Does it have anything to do with the loop i have it in?

    I also changed my cout statement to

    Code:
       cout << ones << " = " << word << endl;

  8. #8
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Well, four things caught my attention.
    1. What kind of datatype is "ones"?
    2. What kind of datatype is "number"?
    3. Why do you call oneplace method without using its return value? (That means your method did nothing to the program)
    4. Why did you use a reference to int for the method's parameter? You didn't plan to change the parameter's value in the method wouldn't you? All you do is just using the parameter as a comparison in if clauses.

    Maybe a better way to solve this is to debug it and add watches to the values of ones, number, and word. Then maybe you can see the flaw of your logic in the code.

  9. #9
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You must do something like this:
    Code:
    string thenum;
    thenum = oneplace(ones);
    cout << ones << " = " << thenum << endl;
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  3. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-02-2008, 12:56 AM
  4. problem with 2d array program.
    By Niloc1 in forum C Programming
    Replies: 1
    Last Post: 04-08-2002, 06:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21