Create Account

Username
Password
Remember me
Email
 
27
mohit_117
mohit_117

Trying to Compile a small C++ program...

14 comments, 196 views, posted 12:30 pm 24/01/2012 in Requests by mohit_117
mohit_117 has 6434 posts, 1895 threads, 196 points, location: India
Good Man di Lantern

Hi!

I'm trying to compile a small program in C++ ... I've not programmed anything since quite some years.. So I'm asking here..

I want to give a digit value to each alphabet of the english letters uptil 9.

A : 1
B : 2
C : 3
D : 4
E : 5
F : 6
G : 7
H : 8
I : 9
J : 1
K :2
L : 3

and so on.... so A to I = 1 to 9 ... J to R = 1 to 9 again... and so on... with Z = 8.

After setting these values, I need to take input from the user for a string. for example: TEOTI
Then I trying to add the lettered values to a total: T + E + O + T + I = 2 + 5 + 6 + 2 + 9 = 24.

So I'm trying to get a total of 24 from this program on a user input of 24.

Please please can someone help me in this??

I've tried a lot with simplest of coding that I remembered but couldn't figure out how to get that total.

Thanks for any help.
-Mohit.

Extra Points Given by:

PiratePoet (25)

Comments

1
12:32 pm 24/01/2012

mohit_117


Here's the cpp file I've copied to my dropbox folder...
http://dl.dropbox.com/u/27854284/teoti/text%20in%20numerals.cpp

1
2:46 pm 24/01/2012

Edorph

Code:
int main() {

  char chars[ SOME_LIMIT ];
  int value = 0;

  fgets( chars, SOME_LIMIT, stdin );

  for( size_t i = 0; i < strlen(chars); i++ )
    value += ( chars[i] % 32 + 8 ) % 9 + 1;

  cout << "Mohit says: " << value << "!" << endl;
  return 0;
}

No guarantees!! If you use the ascii value, you don't need the mile high switch statement that you have :-) I'm sure it can be made prettier than mine though, and obviously this will probably fail miserably on funny characters.

1
3:51 pm 24/01/2012

mohit_117

Thanks edorph!!! I'll compile this tomorrow and reply back... if I make any changes to it, will let you know about that too!!

That value+= sure looks very confusing and impossible simultaneously... I'm wondering how that code can get the output...!

Thanks again!

1
4:32 pm 24/01/2012

Edorph

No worries, happy coding :-) It's a lot of fun to do little things like this, you should look at Project Euler if you want more small (and big!) coding challenges.

I agree the value += ... looks hideous and confusing :-) There are probably other ways to do it.

Basically, the "basic" characters are already numbered (A=65, B=66, C=67 and so on*), but they're not numbered exactly the way you want (A=1, B=2, ...J=9, K=1, L=2 ...).

So what it does is that it transforms 65,66,67... into 1,2,3..9,1,2,3... at least I hope it does :-) Because 'A' (65) and 'a' (97) are 32 apart, it works for both upper and lower case characters.

* See https://duckduckgo.com/?q=ascii+table.

1
4:59 pm 24/01/2012

mohit_117

Ok... I'll check that out...

Also, what data type is size_t i ?? I couldn't understand that...

1
5:13 pm 24/01/2012

Edorph

It's pretty much an unsigned integer, so you can just use an integer instead if you want to. It's often used to denote the size of things because it's guaranteed to be capable of storing the size of the largest object on your platform. The more the compiler knows about your variables, the more it can optimize.

0
5:33 pm 24/01/2012

mohit_117

Nice! Thanks... Would I need any extra "include" files apart from conio.h and iostream.h ??

1
5:41 pm 24/01/2012

Edorph

Quote by mohit_117:
Nice! Thanks... Would I need any extra "include" files apart from conio.h and iostream.h ??


You don't need conio.h. I'm guessing you used that for getch, but it's not standard. I think all you need to include is iostream and stdio.h, and probably string.h for strlen (but you can rewrite the for loop to stop at newline or \0 if you don't want it). And also "using namespace std", which you already had.

Edit: And to define SOME_LIMIT of course (or replace them with a number).

3
6:13 pm 24/01/2012

Flee

What was the point of this code? This smells of a class project that you just got someone else to do

1
8:29 am 25/01/2012

mohit_117

I just compiled it!!
For "Mohit" the result is 30... and for khanna it's 23.
Through my calculations, it should be 29 and 22 respectively....

Think I'll just substract a 1 from the value during "cout<<"

Thanks again!!

1
9:44 am 25/01/2012

Edorph

Weird, that should be 29 and 22. You can put a small cout in the for loop to get an idea of what's wrong. For example:

Code:
for( size_t i = 0; i < strlen(chars); i++ )
{
  cout << chars << " becomes " << (( chars % 32 + 8 ) % 9 + 1) << endl;
  value += ( chars % 32 + 8 ) % 9 + 1;
}
0
10:28 am 25/01/2012

mohit_117

Program created...

http://dl.dropbox.com/u/27854284/teoti/Alphabet%20to%20Numeral%20Sum.exe


code here:

Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;

int main() {

char chars[100], ans;
int value = 0;


system("title Sum Calculation Program for Alphabets");
system("color F0");


cout<<"Enter any random characters to clear the buffer of program... : ";

do
{
char temp;
do scanf("%c", &temp);
while (temp != '\n');

value=0;
system("cls"); //clears the screen
cout<<"\n\n\t****************************************************************";
cout<<"\n\tProgram to Calculate respective values of English Letters (sum)";
cout<<"\n\t****************************************************************";


cout<<"\n\n\n\t*********************";
cout<<"\n\t* 1 2 3 4 5 6 7 8 9 *";
cout<<"\n\t---------------------";
cout<<"\n\t* A B C D E F G H I *";
cout<<"\n\t* J K L M N O P Q R *";
cout<<"\n\t* S T U V W X Y Z *";
cout<<"\n\t*********************";

cout<<"\n\n\tPlease enter a word for calculation : ";

fgets( chars,100, stdin );

for( size_t i = 0; i < strlen(chars); i++ )

if(chars[i] != 32)
value += ( chars[i] % 32 + 8 ) % 9 + 1;
else value = (value + 0);

cout << "\n\n\t\tThe Sum Value is: " << (value - 1)<< " " << "\n\n\n\n\tPress any key to continue...";
getch();

cout<< "\n\n\n\n\n\tDo you want to continue (Y/N)?\n";
cout<< "\tYou must type a 'Y' or an 'N'. : ";
cin >> ans;
}
while((ans =='Y')||(ans =='y'));



return 0;
}
0
10:28 am 25/01/2012

mohit_117

Thanks again edorph!!

0
10:30 am 25/01/2012

mohit_117

Quote by Edorph:
Weird, that should be 29 and 22. You can put a small cout in the for loop to get an idea of what's wrong. For example:


I'll check that in some time... I've already created the program..

I used (value-1) in cout to give the sum... the for loop code was too complicated for me to understand...

Code:
cout << "\n\n\t\tThe Sum Value is: " << (value - 1)

Add Comment

Log in via teoti, or register to add a comment!


Teh NookMayrHayasdanJaranWebAngryShirtsGoneGeekEat Liver