Calculator Help by NEWB.

Hey guys,

I am VERY new to C++ and I'm trying to write a calculator.

The calculator has declared numerical values for alpha characters.

Basically I want to calculate the SUM of the numbers of a name.

For example: my name is "Greg" and my name's value should be, according to the values assigned, 323.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 #include <iostream>

using namespace std;


int main()
{

    int a = 6;
    int b = 18;
    int c = 24;
    int d = 66;
    int e = 35;
    int f = 54;
    int g = 63;
    int h = 72;
    int i = 81;
    int j = 90;
    int k = 99;
    int l = 108;
    int m = 117;
    int n = 126;
    int o = 135;
    int p = 144;
    int q = 153;
    int r = 162;
    int s = 171;
    int t = 180;
    int u = 189;
    int v = 798;
    int w = 807;
    int x = 116;
    int y = 325;
    int z = 934;
    int sum;


    cout << "Enter a name \n";

    cin >> a;



    sum = a + b;
    cout << "The number of the name is \n" << sum << endl;
        return 0;
}
1
2
cout << "Enter a name \n";
cin >> a;
I thought that `a' hold the value for the `a' character, ¿why are you overwritting that with the name?


> sum = a + b;
can understand that you think that `a' now holds the name, ¿buy why do you add `b' to it?
You sort of made a half fast effort, so in the spirit of sharing I've enclosed the following code, but I would seriously recommend not handing it in unless you can explain how and why.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector <int> Char_Weight { 6, 18, 24, 66, 35, 54, 63, 72, 81, 90, 99, 108, 117, 126, \
			135, 144, 153, 162, 171, 180, 189, 798, 807, 116, 325, 934 };
int main (void) {
	string Nme;
	int Value;
	
	cout << "Name: ";
	cin >> Nme;
	
	if ( !Nme.empty () ) {
		Value = 0;
		
		for ( auto ch : Nme ) {
			int Weight = int ( toupper (ch) - 'A');
			Value += Char_Weight [Weight];
		}
		
		cout << " Weight value = " << Value;
	}
	
	return 0;
}
Well thanks for not being harsh guys. When I said I was NEW, I meant it. I started about 5 hours ago. So please, maybe we could be a little more understanding???? Seriously, 5 hours ago.



cout << "Enter a name \n";
cin >> a;


I thought that `a' hold the value for the `a' character, ¿why are you overwritting that with the name?

>>>> YES, these are questions I want answered as well. Like I said, I started programming C++, ehhh, five hours ago. Never programmed before, so looking for a little help is all.


Again, I have no idea. I am learning, hence the asking for help. ;)-----------------> sum = a + b;
can understand that you think that `a' now holds the name, ¿buy why do you add `b' to it?
Topic archived. No new replies allowed.