I'm rusty and I need help

This is a discussion on I'm rusty and I need help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <stdlib.h> #include <string> using namespace std; int main() { string num; int x = 0; cout ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    Talking I'm rusty and I need help

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    
    int main()
    {
          string num;
          int x = 0;
          cout << "Input a Number.\n";
          cin >> num;
          x = strlen('num');
          string numLast = num.substr(x, -1);
          switch ('numLast')
          {
          case '1': cout << num << "st.\n";
          break;
          case '2': cout << num << "nd.\n";
          break;
          case '3': cout << num << "rd.\n";
          default: cout << "\n" << num << "th.\n";
          }
          system("PAUSE");
          return 0;
    }
    This is basically a going to be a function that returns wether a number is a st, nd, rd or a th. If ya know what I mean.

    Any help is appreciated. Thanks.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    don't put ' ' around variable names:
    Code:
    x = strlen('num');
    //should be
    x = strlen(num);
    and the same goes for the switch statement
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    It's giving me 9 errors now.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    strlen is for C-style strings (char *) only. The C++ string class has a length() function. switch statements take an integral type as an argument, so a string would not work (a single character is an integral type though).

    It would be useful if you would post exactly what error messages you are getting.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    This is the code:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    
    int main()
    {
          int x = 0;
          string num;
          cout << "Input a Number.\n";
          cin >> num;
          x = length(num);
          string numLast = num.substr(x, -1);
          switch (numLast)
          {
          case '1': cout << num << "st.\n";
          break;
          case '2': cout << num << "nd.\n";
          break;
          case '3': cout << num << "rd.\n";
          default: cout << "\n" << num << "th.\n";
          }
          system("PAUSE");
          return 0;
    }
    These are the errors:
    1. implicit declaration of function `int length(...)'
    2. warning: cannot pass objects of type `string' through `...'
    3. switch quantity not an integer
    4. case label `'1'' not within a switch statement
    5. break statement not within loop or switch
    etc.

    I know I'm being annoying, but it's just that PHP's messed up my C++ comprehension. PHP code would look like this:
    Code:
    function nSufx ($n) 
    { 
      switch (substr($n,-1)) 
      { 
         case 1: return $n.'st'; 
         case 2: return $n.'nd'; 
         case 3: return $n.'rd'; 
         default: return $n.'th'; 
      } 
    }
    Just in case you were wondering why my code sucks.
    Last edited by phay; 08-12-2003 at 09:54 PM.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    for "using namespace std;" to work you will need to include the right headers:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    int main()
    {
    
    string num;//why is this a string?
    int x=0;
    cout << "Input a Number.\n";
    
    cin >> num;  
      
       x = num.length();//finding length of a string object (rather than char*)
          string numLast = num.substr(x, -1); //-1 wont work and i dont see why you are doing this
          switch (numLast) //you cant use a string here
          {
          case '1': cout << num << "st.\n";
          break;
          case '2': cout << num << "nd.\n";
          break;
          case '3': cout << num << "rd.\n";
          default: cout << "\n" << num << "th.\n";
          }
          system("PAUSE");
          return 0;
    }
    
    
    return 0
    }
    substring will take a string out of a string like:
    Code:
    string one("stringone");
    string two("two");
    string x;
    x= one.substr(6)+two.substr(2);
    //x equals "oneo"
    or your code will take a string out of the center (except that you use a negative value)

    so the real question is what are you trying to do since I don't understand your code at all...and the code i just posted wont work either
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    From the looks of it, what you want is:
    Code:
    int x = num.length();
    char numLast = num[x - 1]; // Or equivalent expression...
    switch(numLast)
    { ... etc ... }
    A char is an integral type, so that will work.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

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