Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 07-15-09   #1 (permalink)
PC Gamer
 
Snogus's Avatar
 
intel nvidia

Join Date: Dec 2008
Location: Australia
Posts: 151
Blog Entries: 4

Rep: 6 Snogus Unknown
Unique Rep: 6
Folding Team Rank: 624
Trader Rating: 0
Angry [C++] Help needed, basics gone bad

Hi all,
Created a C++ app and its not turning letters into numbers right e.g. a=1 b=2 c=3.
Keep in mind im VERY new to this.
this is what i get (attachment)


Sorry bout the amount of code, i spagghettied it just to get it running first, i'll clean it up when i get it working.

Here is my code:
Code:
#include <iostream>
#include <math.h>

using namespace std;

int main(void)
{
    char name4l1;
    char name4l2;
    char name4l3;
    char name4l4;
    int name4no1;
    int name4no2;
    int name4no3;
    int name4no4;
    const char a = a;
    const char b = b;
    const char c = c;
    const char d = d;
    const char e = e;
    const char f = f;
    const char g = g;
    const char h = h;
    const char i = i;
    const char j = j;
    const char k = k;
    const char l = l;
    const char m = m;
    const char n = n;
    const char o = o;
    const char p = p;
    const char q = q;
    const char r = r;
    const char s = s;
    const char t = t;
    const char u = u;
    const char v = v;
    const char w = w;
    const char x = x;
    const char y = y;
    const char z = z;
    cout << "       2222       3333" << endl;
    cout << "      22  22     33  33" << endl;
    cout << "         22         33" << endl;
    cout << "        22       33  33" << endl;
    cout << "      222222      3333" << endl;   
    cout << "         ENIGMAFIER" << endl;
    cout << " " << endl;
    cout << "Welcome to the 23 Enigmafier" << endl;
    cout << "Please enter a four letter name: ";
    cin >> name4l1;
    cin >> name4l2;
    cin >> name4l3;
    cin >> name4l4;
    
    if(name4l1 == a){
                name4no1 = 1;
                }
    if(name4l1 == b){
                name4no1 = 2;
                }
    if(name4l1 == c){
                name4no1 = 3;
                }
    if(name4l1 == d){
                name4no1 = 4;
                }
    if(name4l1 == e){
                name4no1 = 5;
                }
    if(name4l1 == f){
                name4no1 = 6;
                }
    if(name4l1 == g){
                name4no1 = 7;
                }
    if(name4l1 == h){
                name4no1 = 8;
                }
    if(name4l1 == i){
                name4no1 = 9;
                }
    if(name4l1 == j){
                name4no1 = 10;
                }
    if(name4l1 == k){
                name4no1 = 11;
                }
    if(name4l1 == l){
                name4no1 = 12;
                }
    if(name4l1 == m){
                name4no1 = 13;
                }
    if(name4l1 == n){
                name4no1 = 14;
                }
    if(name4l1 == o){
                name4no1 = 15;
                }
    if(name4l1 == p){
                name4no1 = 16;
                }
    if(name4l1 == q){
                name4no1 = 17;
                }
    if(name4l1 == r){
                name4no1 = 18;
                }
    if(name4l1 == s){
                name4no1 = 19;
                }
    if(name4l1 == t){
                name4no1 = 20;
                }
    if(name4l1 == u){
                name4no1 = 21;
                }
    if(name4l1 == v){
                name4no1 = 22;
                }
    if(name4l1 == w){
                name4no1 = 23;
                }
    if(name4l1 == x){
                name4no1 = 24;
                }
    if(name4l1 == y){
                name4no1 = 25;
                }
    if(name4l1 == z){
                name4no1 = 26;
                }
    if(name4l2 == a){
                name4no2 = 1;
                }
    if(name4l2 == b){
                name4no2 = 2;
                }
    if(name4l2 == c){
                name4no2 = 3;
                }
    if(name4l2 == d){
                name4no2 = 4;
                }
    if(name4l2 == e){
                name4no2 = 5;
                }
    if(name4l2 == f){
                name4no2 = 6;
                }
    if(name4l2 == g){
                name4no2 = 7;
                }
    if(name4l2 == h){
                name4no2 = 8;
                }
    if(name4l2 == i){
                name4no2 = 9;
                }
    if(name4l2 == j){
                name4no2 = 20;
                }
    if(name4l2 == k){
                name4no2 = 21;
                }
    if(name4l2 == l){
                name4no2 = 22;
                }
    if(name4l2 == m){
                name4no2 = 23;
                }
    if(name4l2 == n){
                name4no2 = 24;
                }
    if(name4l2 == o){
                name4no2 = 25;
                }
    if(name4l2 == p){
                name4no2 = 26;
                }
    if(name4l2 == q){
                name4no2 = 27;
                }
    if(name4l2 == r){
                name4no2 = 28;
                }
    if(name4l2 == s){
                name4no2 = 29;
                }
    if(name4l2 == t){
                name4no2 = 20;
                }
    if(name4l2 == u){
                name4no2 = 22;
                }
    if(name4l2 == v){
                name4no2 = 22;
                }
    if(name4l2 == w){
                name4no2 = 23;
                }
    if(name4l2 == x){
                name4no2 = 24;
                }
    if(name4l2 == y){
                name4no2 = 25;
                }
    if(name4l2 == z){
                name4no2 = 26;
                }
    if(name4l3 == a){
                name4no3 = 1;
                }
    if(name4l3 == b){
                name4no3 = 2;
                }
    if(name4l3 == c){
                name4no3 = 3;
                }
    if(name4l3 == d){
                name4no3 = 4;
                }
    if(name4l3 == e){
                name4no3 = 5;
                }
    if(name4l3 == f){
                name4no3 = 6;
                }
    if(name4l3 == g){
                name4no3 = 7;
                }
    if(name4l3 == h){
                name4no3 = 8;
                }
    if(name4l3 == i){
                name4no3 = 9;
                }
    if(name4l3 == j){
                name4no3 = 10;
                }
    if(name4l3 == k){
                name4no3 = 11;
                }
    if(name4l3 == l){
                name4no3 = 12;
                }
    if(name4l3 == m){
                name4no3 = 13;
                }
    if(name4l3 == n){
                name4no3 = 14;
                }
    if(name4l3 == o){
                name4no3 = 15;
                }
    if(name4l3 == p){
                name4no3 = 16;
                }
    if(name4l3 == q){
                name4no3 = 17;
                }
    if(name4l3 == r){
                name4no3 = 18;
                }
    if(name4l3 == s){
                name4no3 = 19;
                }
    if(name4l3 == t){
                name4no3 = 20;
                }
    if(name4l3 == u){
                name4no3 = 23;
                }
    if(name4l3 == v){
                name4no3 = 22;
                }
    if(name4l3 == w){
                name4no3 = 23;
                }
    if(name4l3 == x){
                name4no3 = 24;
                }
    if(name4l3 == y){
                name4no3 = 25;
                }
    if(name4l3 == z){
                name4no3 = 26;
                }
    if(name4l4 == a){
                name4no4 = 1;
                }
    if(name4l4 == b){
                name4no4 = 2;
                }
    if(name4l4 == c){
                name4no4 = 3;
                }
    if(name4l4 == d){
                name4no4 = 4;
                }
    if(name4l4 == e){
                name4no4 = 5;
                }
    if(name4l4 == f){
                name4no4 = 6;
                }
    if(name4l4 == g){
                name4no4 = 7;
                }
    if(name4l4 == h){
                name4no4 = 8;
                }
    if(name4l4 == i){
                name4no4 = 9;
                }
    if(name4l4 == j){
                name4no4 = 10;
                }
    if(name4l4 == k){
                name4no4 = 11;
                }
    if(name4l4 == l){
                name4no4 = 12;
                }
    if(name4l4 == m){
                name4no4 = 13;
                }
    if(name4l4 == n){
                name4no4 = 14;
                }
    if(name4l4 == o){
                name4no4 = 15;
                }
    if(name4l4 == p){
                name4no4 = 16;
                }
    if(name4l4 == q){
                name4no4 = 17;
                }
    if(name4l4 == r){
                name4no4 = 18;
                }
    if(name4l4 == s){
                name4no4 = 19;
                }
    if(name4l4 == t){
                name4no4 = 20;
                }
    if(name4l4 == u){
                name4no4 = 21;
                }
    if(name4l4 == v){
                name4no4 = 22;
                }
    if(name4l4 == w){
                name4no4 = 23;
                }
    if(name4l4 == x){
                name4no4 = 24;
                }
    if(name4l4 == y){
                name4no4 = 25;
                }
    if(name4l4 == z){
                name4no4 = 26;
                }
    
    cout << "Letter 1 = ";
    cout << name4no1 << endl;
    cout << "Letter 2 = ";
    cout << name4no2 << endl;
    cout << "Letter 3 = ";
    cout << name4no3 << endl;
    cout << "Letter 4 = ";
    cout << name4no4 << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Attached Thumbnails
[C++] Help needed, basics gone bad-error.jpg  
__________________
Please note:
Stuart (A.K.A. Overlord), you are evil (you know who you are)
Ninjas beat Pirates
Ninjas beat Samurai
Chuck Norris draws even with the best Ninjas
Ninjas work on the deli system (The old one, the new one is just whacked)
IT People do little to no work (that's why it's a great line of work)
Ninjas beat Pirates (just to make sure you know)


System: Uber-PC
CPU
Intel E8400 Core 2 Duo @ 3.00 GHz
Motherboard
ASUS P5Q
Memory
2GB Corsair Dominator
Graphics Card
ASUS 8800GT
Hard Drive
500GB
Sound Card
8.1 On-Board
Power Supply
Antec NEO 450w
Case
Antec 900 Gaming Case
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Pro 32 bit
Monitor
Chimei 22" Widescreen
Snogus is offline I fold for Overclock.net Snogus's Gallery   Reply With Quote
Old 07-15-09   #2 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: egypt
Posts: 1,267

Rep: 67 Pooping^fish is acknowledged by some
Unique Rep: 62
Trader Rating: 3
Default

Theres quite a bit wrong here..
First of all, when working with a single char you use single quotes.
Example
Code:
char A = 'a';
It would be better to use a switch on the inputted letters,or even better yet taking it in as an array and switching on it, incrementing the index.

Well, I got bored. Heres a functioning example in C. Take in mind, this could be done quite a few ways.

Code:
#include <stdio.h>
#include <stdlib.h>
#define A 'a'
#define B 'b'
#define C 'c'

int main()
{
  char MyString[] = "abc";
  unsigned short MyBuf[strlen(MyString)];
  
  int x;
  for(x = 0; x < strlen(MyString); ++x) {
    switch (MyString[x]) {
        case A:
             MyBuf[x] = 1;
             break;
        case B:
             MyBuf[x] = 2;
             break;
        case C:
             MyBuf[x] = 3;
             break;
        default:
          printf("Unknown char.\n");
          MyBuf[x] = 0;
                        }
  }
  printf("%s converted is ", MyString);
  for(x = 0; x < strlen(MyString); ++x) {
        printf("%d", MyBuf[x]); }
  printf("\n");
  
  getch();	
  return 0;
}
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: ragequit
CPU
Q9550 4ghz @ 1.25v
Motherboard
Asus Max 2 formula
Memory
OCZ LV blade 1:1 950mhz
Graphics Card
8800gtx 610/1ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
leetlinucks
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Old 07-15-09   #3 (permalink)
:wheee:
 
mrtn400's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: Carmichael, CA
Posts: 4,363
Blog Entries: 2

Rep: 279 mrtn400 is a proven membermrtn400 is a proven membermrtn400 is a proven member
Unique Rep: 237
Hardware Reviews: 1
Trader Rating: 1
Default

Yeah, like Pooping^Fish said you should really look into some arrays and for loops. It'll make your life 100 times easier.

And here's my example in C++ with char arrays and for loops:
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{ 
    // temporarialy store the name in a string so that the char array will
    // be the correct length
    string tempname;
    cout<<"Please enter your name:"<<endl;
    getline(cin, tempname);
    
    char name[tempname.size()];
    strcpy(name, tempname.c_str());
    
    for(int i=0; i < strlen(name); i++)
    {
            // if the char is an uppercase letter...
            if((int)name[i] >= 65 && (int)name[i] <= 90)
            {
                            // ... make it lowercase
                            name[i] = (char)((int)name[i] + 32);
            }
    }
    
    int sum=0;
    for(int i=0; i < strlen(name); i++)
    {
            // if the char is a lowercase letter...
            if((int)name[i] >= 97 && (int)name[i] <= 122)
            {
                            cout<<"Letter "<<i<<": "<<name[i]<<" = "<<(int)name[i]-96<<endl;
                            
                            // ... give it the correct value and add it
                            sum += (int)name[i] - 96;
            }
            
            else if((int)name[i] >= 65 && (int)name[i] <= 90)
            {
                 cout<<"Error: Posisition "<<i<<" of name is still uppercase ("
                     <<name[i]<<")!"<<endl;
                 cin.get();
                 return 1;
            }
    }
    
    cout<<"Your name adds up to "<<sum<<".";
    
    cin.get();
    return 0;
}
__________________
System: Evolution (Alt Rig)
CPU
2x 3.06Ghz Xeon Prestonia (SL6VP) w/ HT off
Motherboard
MSI E7505 Master-LS2
Memory
4x 512MB Registered DDR-266
Graphics Card
BFG 7300GT 512MB @ 590Mhz Core/380Mhz Memory
Hard Drive
Hitachi 120GB
Sound Card
ASUS Xonar DX w/ shielding
Power Supply
450w
Case
Stock IBM
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Professional SP3
Monitor
1x Acer 19" @ 1400x900, 1x Sceptre 19" @ 1280x1024
mrtn400 is offline Overclocked Account mrtn400's Gallery   Reply With Quote
Old 07-15-09   #4 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Oooh! A late night challenge...fun stuff...

Code:
std::string GetNameFromInput()
{
    // TODO..
    // Hardcoded for now.s
    return "Rich";
}

void DisplayResultToOutput(const std::string& name, int sum)
{
    // TODO:
}

int main()
{
    std::string name = GetNameFromInput();

    int sum = 0;
    for (const char* p = name.c_str(); *p != '\0'; ++p)
        sum += tolower(*p) - 'a' + 1;;
    
    DisplayResultToOutput(name, sum);
	return 0;
}
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 07-16-09   #5 (permalink)
PC Gamer
 
Snogus's Avatar
 
intel nvidia

Join Date: Dec 2008
Location: Australia
Posts: 151
Blog Entries: 4

Rep: 6 Snogus Unknown
Unique Rep: 6
Folding Team Rank: 624
Trader Rating: 0
Default

Thanks heaps you guys, you've given me some great ideas.
I was using data arrays but when it wasn't working i decided to bail on them because it was the first time i had used them, just in case it was the problem. After looking at your suggestions i think i'm up for a full re-write, I go to a Catholic high school so i write it up with pen and paper in religion classes.

I'm repping you all up btw. kickass help
__________________
Please note:
Stuart (A.K.A. Overlord), you are evil (you know who you are)
Ninjas beat Pirates
Ninjas beat Samurai
Chuck Norris draws even with the best Ninjas
Ninjas work on the deli system (The old one, the new one is just whacked)
IT People do little to no work (that's why it's a great line of work)
Ninjas beat Pirates (just to make sure you know)


System: Uber-PC
CPU
Intel E8400 Core 2 Duo @ 3.00 GHz
Motherboard
ASUS P5Q
Memory
2GB Corsair Dominator
Graphics Card
ASUS 8800GT
Hard Drive
500GB
Sound Card
8.1 On-Board
Power Supply
Antec NEO 450w
Case
Antec 900 Gaming Case
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Pro 32 bit
Monitor
Chimei 22" Widescreen
Snogus is offline I fold for Overclock.net Snogus's Gallery   Reply With Quote
Old 07-17-09   #6 (permalink)
nVidia Enthusiast
 
intel nvidia

Join Date: Feb 2007
Posts: 212

Rep: 1 xSevasTra Unknown
Unique Rep: 1
Trader Rating: 0
Default

I'd use a while loop with the to upper function in there to help clean it up, would also make it easier to read.
__________________


Because perfect hair is overrated....

System: My System
CPU
| e8400 | 4050mhz |
Motherboard
| eVGA 680i NF68-A1 | vMod |
Memory
| 4GB | OCZ BLADE | 1106 | 5-5-5-15 |
Graphics Card
| BFG GTX 275 |
Hard Drive
| 2 | 500GB | Western Digital | Caviar |
Power Supply
| Silverstone Zues | 850w |
Case
| Lian Li PC-61B |
CPU cooling
| Prolimatech | Megashadow | 2 150CFM Delta |
Monitor
| Samsung 2333SW | 23" |
xSevasTra is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 06:52 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.13395 seconds with 9 queries