|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
[C++] Help needed, basics gone bad
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
PC Gamer
|
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;
}
__________________
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) ![]()
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
Theres quite a bit wrong here..
First of all, when working with a single char you use single quotes. Example Code:
char A = 'a'; 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:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
:wheee:
![]() |
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;
}
|
|||||||||||||
|
|
|
|
#4 (permalink) |
|
Case Modder
![]() |
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
|
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
PC Gamer
|
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) ![]()
|
|||||||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||
|
nVidia Enthusiast
|
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.
|
||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|