More C++ Help

Discussion in 'Gaming' started by White B O I, Feb 5, 2009.

  1. White B O I

    White B O I Well-Known Member

    Age:
    34
    Posts:
    813
    Likes Received:
    0
    Joined:
    Apr 11, 2006
    Location:
    Table Town, Arizona
    I'm making a program that converts letters to number

    the example is that you input something like VOTENOW and return 8683663

    here is my code

    Code:
    // 3.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {    
        string letter, temp[7];
        int i,x;
        
        i=0;
    
        cout << "Enter a combination of letters (max 7)";
        cin >> letter;
    
        x = letter.size();
    
        while (x < 7)
        {
            temp[i] = letter.substr(i,1);
            if (temp[i] < "d")
                temp[i] = "2";
            else if (temp[i] < "g")
                temp[i] = "3";
            else if (temp[i] < "j")
                temp[i] = "4";
            else if (temp[i] < "m")
                temp[i] = "5";
            else if (temp[i] < "p")
                temp[i] = "6";
            else if (temp[i] < "t")
                temp[i] = "7";
            else if (temp[i] < "w")
                temp[i] = "8";
            else
                temp[i] = "9";
    
            i = i + 1;
    
            }
            
            cout << temp << endl;
        
        
        return 0;
    }
    I don't get any errors, but no matter what I put in i get this back

    And I have no idea why.
     
  2. .c1

    .c1 Well-Known Member

    Posts:
    824
    Likes Received:
    0
    Joined:
    Aug 5, 2007
    Can you just cout and array like that?


    cout << temp?

    wouldn't be cout << temp[0] and etc......

    Im installing VB 2005 right now, so I will check when down

    edit and if I remember correctly it printing out the location in memory or something not the value.


    EDIT

    Wait,

    Code:
          if (temp[i] < "d")
            {
                temp[i] = "2";
            }
            else if (temp[i] < "g")
            {
                temp[i] = "3";
            }
         
            /* Code Omitted */

    if (temp < "d")

    In the temp there's a letter right, so a letter cant be greater than another letter.

    And in the while loop here:

    Code:
        while (x < 7)
        {
    
    
            i = i + 1;
    
         }
    There no increment for the x, so if x is less than 7, it's an infinite loop. Or the loop wont run, if x is greater than 7.



    Code:
            [size="3"]cout << temp << endl;[/size]
    ^^ yeah you cant do that, you need to use the array index. So you should do an for loop to go through and cout each value in the array index.

    Some more stuff as well...will likely edit in the later.
     
  3. AeroSwift

    AeroSwift Member

    Posts:
    19
    Likes Received:
    0
    Joined:
    Jan 17, 2009
    @.c1: No, you can do that. It will display everything in the array. I have done it several times.
    Also, I'm not sure, but:
    1) Chars don't use "", but ''.
    2) Aren't there {}'s involved with if ... else (if) loops?
     
  4. White B O I

    White B O I Well-Known Member

    Age:
    34
    Posts:
    813
    Likes Received:
    0
    Joined:
    Apr 11, 2006
    Location:
    Table Town, Arizona
    Not necessary, you don't need to use {} unless its a block, but I fixed the problem.. got rid of the array.. and it worked :D


    and in C++ you can compare numbers, so a < b and A > b (I think it works with caps like that) i'm not positive on the system
    but I know for sure that a < b < c < ... < z at least in c++
     
  5. .c1

    .c1 Well-Known Member

    Posts:
    824
    Likes Received:
    0
    Joined:
    Aug 5, 2007
    Oh well my mistake, didnt know about comparing string like that.

    And aero.swift I tried to cout out a simple array I made yesterday and it still gave me a location, instead of the actually array.
     

Share This Page