English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Write a C++ program that enters an 8-digit string for a birthdate from keyboard. The first two digits in the string are month of birth, the next two are the day and the remaining four are the years. The C++ should squeeze out these substrings and display it on the screen as follows:

month of birth:
day of birth:
year of birth:

2007-02-07 00:13:39 · 5 answers · asked by texas87x 1 in Computers & Internet Programming & Design

5 answers

#include
#include
using namespace std;

int ctoi(string aString, int index)
{
return (int)(aString[index-1]-'0');
}

//program mmddyyyy
int main()
{
int month,day,year;
string strData;

while(true) {

system("cls");
cout << "\nEnter a string : ";
cin >> strData;


month = ctoi(strData, 1) *10 + ctoi(strData, 2);
day = ctoi(strData,3) * 10 + ctoi(strData,4);
year = ctoi(strData, 5) * 1000 + ctoi(strData, 6) * 100 + ctoi(strData, 7) * 10 + ctoi(strData, 8);

cout << "\nmonth of birth : " << month << endl;
cout << "day of birth : " << day << endl;
cout << "year of birth : " << year << endl;
cout << endl << endl;
system("pause");
}
return 0;
}

2007-02-07 07:25:46 · answer #1 · answered by iyiogrenci 6 · 0 0

your program seems to just be converting a single digit of decimal into hexadecimal (kind of more of just renaming 10 - 15 A - F) rather than going from hexadecimal to decimal if you wanted to go from hexadecimal to decimal you could do something along the lines of int A = 10, B = 11, ...; then take the input that they give you and then have cin >> hexa; n = digit; deci += hexa * pow(16, n); using a loop to make sure you get every digit or something if you know the number of digits you can start from the highest one otherwise you have to start from the lowest one unless you want to get the entire input first

2016-05-24 02:40:22 · answer #2 · answered by Anonymous · 0 0

#include
#include
using namespace std;

int ctoi(string aString, int index)
{
return (int)(aString[index-1] - '0');
}

int main()
{
string strData;
int month = ctoi(strData, 8) *10 + ctoi(strData, 7);
int day = ctoi(strData,6) * 10 + ctoi(strData,5);
int year = ctoi(strData, 4) * 1000 + ctoi(strData, 3) * 100 + ctoi(strData, 2) * 10 + ctoi(strData, 1);
cout << month << endl;
cout << day << endl;
cout << year << endl;
return 0;
}

2007-02-07 01:19:00 · answer #3 · answered by platobungee 2 · 0 1

SeriouslY!

I agree.

Come on! do your homework yourself!

Be a Man! Do the right thing!

2007-02-07 00:31:01 · answer #4 · answered by shrek 5 · 1 0

If you need omeone else to do your homework, then you should switch majors...

2007-02-07 00:22:40 · answer #5 · answered by MenifeeManiac 7 · 1 0

fedest.com, questions and answers