I'm a beginner programmer and I'm trying to make a add function that adds two numbers. but these numbers are passed by strings.
ex. four + five = is 9.
if the answer goes over 10m then it prints out "error".
can anyone help me ??

int add (string number1, string number2)
{
			string Manta[10] =   {"one","two","three","four","five","six","seven","eight","nine","ten"};
			
if( ??? )
{
}
else
    cout << "ERROR!" << endl;

}

Recommended Answers

All 10 Replies

Hint: "one" is at position 0 of the Manta array, "two" is at position 1, etc.

What do you mean by '10m'?

My guess would be 10.0e+6 (10-million). But you're right, that is a strange way to indicate the desired limit...

It could also be 10.0e+3 (10,000), but that would generally be 10k.

If '10m' means 10 million ,as suggested by Fbody, then your if statement should test whether the value is <= 10000000... else print your error message....

Since he is a begginer, I would suppose he means once it is over ten with the m being a typo. Put 'Manta' as an enum. I think begginers know of that. That would allow the variable to be a predefined value only. you could then write a separate function to convert that string value into a numerical value and then do the calculation.

sorry for causing so much trouble! the "m" there was just a typo.

so would it be
if(Manta[x] <= 10) ??

If your only going to use those 9 numbers id probably use a simple loop to test each string if it exists

int int1,int2, answer;

for(int i=0;i<10;i++)
{
  if     (string1==manta[i]){ int1 = i; }
  else if(string2==manta[i]){ int2 = i; }
}

answer = int1+int2;

if(answer>10){ cout<<"Error"; }
else         return mantra[answer+1]; //or just answer if you want it to return a int

This is what i was showing you. Since you have to use strings to input the data, you could do something close to lines 1-4 and then do the calculation like in line 6

if(Manta[loop]='two')
{
    num2=1;
}

sum=num2=num8;

This is what i was showing you. Since you have to use strings to input the data, you could do something close to lines 1-4 and then do the calculation like in line 6

if(Manta[loop]='two')

if(Manta[loop]=="two")

if(Manta[loop]=="two")

Yes that is correct. My mistake there, but you get the idea

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.