Hey i am new to this forum.
I am also new to C++.
I really need help with this program because i cant get it to work!
Since its my first time posting, if I am posting in the wrong way please tell me.
My program holds phone numbers.
uhm the program functions like the following:
1 to enter data.
2 to view data.
1 :
ask for id..store id
ask for name..store name[id]
ask for phonenumber...store phonenumber[id]
2:
ask for id...store id as a dif variable
( i am using dif variable because
i dont want it to collide with the variable i am using in case 1 for storing id)
show name[dif varriable]
Want to show the the phone number as area code, middle 3 digits and last 4 because in the future i want to give capabilities like show area code. For example:
pn = phone number
area code = pn[1], pn[2], pn[3]
middle = pn[4], pn[5]....etc
But....
***(calculate length of phone number)
if length of number is 10:
show the number with the country code(1) preceding it
if length of number is 11:
just show number
*****
**** thats the part i am having trouble with
Code:
Libraries:
#include <iostream>
#include <windows.h>
#include <string>
Variables:
int x; //holds id for entering data
int y; //holds id for viewing data
int z; // yes or no...go to mainmenu.
int i; // length of phonenumber
Functions:
void clearscreen()
{
system("cls");
}
void timedelay()
{
Sleep(5); //increase to slow down termination
}
void termination() // just makes a cool closing
{
clearscreen();
cout << "T";
timedelay();
cout << "h";
timedelay();
cout << "a";
timedelay();
cout << "n";
timedelay();
cout << "k";
timedelay();
cout << " y";
timedelay();
cout << "o";
timedelay();
cout << "u";
timedelay();
cout << " f";
timedelay();
cout << "o";
timedelay();
cout << "r";
timedelay();
cout << " u";
timedelay();
cout << "s";
timedelay();
cout << "i";
timedelay();
cout << "n";
timedelay();
cout << "g";
timedelay();
cout << " t";
timedelay();
cout << "h";
timedelay();
cout << "i";
timedelay();
cout << "s";
timedelay();
cout << " p";
timedelay();
cout << "r";
timedelay();
cout << "o";
timedelay();
cout << "g";
timedelay();
cout << "r";
timedelay();
cout << "a";
timedelay();
cout << "m.";
timedelay();
timedelay();
}
Namespace:
using namespace std;
Actual Code:
int main() {
char a[] = "1234567890";
countrycode = 1;
Mainmenu:
cout << "Press 1 to add entry.\n";
cout << "Press 2 to view entries.\n";
cout << "Press 3 to quit the program.\n";
cin >> choice;
switch(choice){
case 1:
clearscreen();
cout << "Please enter id.\n";
cin >> x;
cout << "Please enter name.\n";
getchar();
getline(cin, name[x]);//holds in name[x]
cout << "Please enter number.NO HYPHENS!\n";
cin >> phonenumber[x];//holds in phonenumber[x]
clearscreen();
cout << "Data has been entered.\n";//clears screen couts data has been entered and goes back to mainmenu
cout << "\n\n\n";
goto Mainmenu;
break;
case 2:
clearscreen();
cout << "Please enter id.\n";
cin >> y;// y is id entered vs x
cout << name[y];//show name according to id entered
cout << "\n";
for(j=0; j<100;j++){
b[j][j] = phonenumber[j][j];
}
i = strspn(b[j], a);//avoid using main string in strspn
cout << i;
cout << "\n";
switch(i){
case 11:
cout << "Phone number: ";
cout << countrycode << phonenumber[y][0] << phonenumber[y][1] << phonenumber[y][2] << "-";//areacode
cout << phonenumber[y][3] << phonenumber[y][4] << phonenumber[y][5] << "-"; // middle 3 digits
cout << phonenumber[y][6] << phonenumber[y][7] << phonenumber[y][8] << phonenumber[y][9];
cout << "\n";
goto Continue;
break;
default:
cout << "Phone number: ";
cout << phonenumber[y][0] << phonenumber[y][1] << phonenumber[y][2] << "-";//areacode
cout << phonenumber[y][3] << phonenumber[y][4] << phonenumber[y][5] << "-"; // middle 3 digits
cout << phonenumber[y][6] << phonenumber[y][7] << phonenumber[y][8] << phonenumber[y][9];
cout << "\n";
goto Continue;
break;
}
Continue:
cout << "Go to mainmenu?[1 for yes/ 2 for no]";
cout << "\n";
cin >> z;
switch(z){
case 1: // yes or no
clearscreen();
goto Mainmenu;
break;
default:
clearscreen();
goto Endofprogram;
break;
}//end of sub switch in case 2: of main switch
break;
case 3:// Quit program option
clearscreen();
goto Endofprogram;
break;
default://clear screen and go to main menu if other than 1 or 2 is pressed
clearscreen();
cout << "That is not a valid choice.\n";
cout << "Please try again.\n";
cout << "\n\n\n";
goto Mainmenu;
break;
} // end of main switch
Endofprogram:
termination();
cout << "\n";
return 0;
}
Also, everyone says to avoid goto statements. Can someone tell me alternates to using goto? Like could you give me an example of where I used goto and show me another way.
Thanks to anyone and everyone that helps.