Character to number conversion

C

cbing

I am trying to write a program the converts letters to
numbers. My attached code works for the first letter
entered but will not read past the first number. Please
help.

Thanks.

#include <iostream>
#include <string>

using namespace std;

void convertnum (char, int&);

int main()
{
char firstnum;
//char secnum;
int firstconv = 0;
//int secconv;


cout << " Please enter the first number: " << endl;
cin >> firstnum;

convertnum (firstnum, firstconv);

cout << " The first number is: " << firstconv <<
endl;


return 0;
}

//*********************************************************
**************************************

void convertnum(char rnum, int& numconv)
{
numconv = 0;

if (rnum == 'M' || rnum == 'm')
numconv += 1000;
if (rnum == 'D' || rnum == 'd')
numconv += 500;
if (rnum == 'C' || rnum == 'c')
numconv += 100;
if (rnum == 'L' || rnum == 'l')
numconv += 50;
if (rnum == 'X' || rnum == 'x')
numconv += 10;
if (rnum == 'V' || rnum == 'v')
numconv += 5;
if (rnum == 'I' || rnum == 'i')
numconv += 1;

}
 
J

Jonathan Mcdougall

I am trying to write a program the converts letters to
numbers. My attached code works for the first letter
entered but will not read past the first number. Please
help.

Look up "while" and "for" in your book/website.


Jonathan
 
R

Ron Natalie

cbing said:
I am trying to write a program the converts letters to
numbers. My attached code works for the first letter
entered but will not read past the first number. Please
help.
That's all it does. It reads one char, converts it, outputs
it, and then returns from main. Perhaps you wanted a loop?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top