Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
help me output is not coming in correct....

#include <iostream>
#include <string>
#include<conio.h>
using namespace std;
int main()
{

string first,second;
cout<<"Enter the first number"<<endl;;
cin>> first; // input=123
cout<<endl<<"Enter the Second Number"<<endl;
cin>> second; // input=324

int product= (int) first[2] * (int)second[2];
cout<<product; //output must be 12 because 3*4=12 please



getch();
return 0;
}
Posted
Updated 30-Aug-11 20:31pm
v3
Comments
Philippe Mori 5-Sep-11 9:22am    
Badly formatted question. Also, uses <pre> block for code.

You are trying to multiply the character code (0x33*0x34, stands for '3' and '4'). If you want to do so:
int product= (int) (first[2]-'0') *   (int)(second[2]-'0');

good luck.
 
Share this answer
 
Comments
Philippe Mori 26-Aug-11 18:27pm    
Better to works with numbers... as it would be less sensible to the actual input formatting (for example 123.0 or 0123)
mbue 26-Aug-11 21:32pm    
Maybe, i dont know why the op wants to multiply two random digits. He only forgot that a digit in a string isnt equal to its numeric equivalent.
C++
atoi(first.c_str())


But you can just change the type of first and second and let cin handle it:
C#
int first
cin >> first;
 
Share this answer
 
string first2 = first[2];
string second2 = second[2];
int product= atoi(first2.c_str()) * atoi(second2.c_str()) ;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900