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.

Recommended Answers

All 10 Replies

1. Always define you vars.
2. Array index always begin with 0.
3. I always can't write English better..Sorry


cout << "Phone number: ";
cout << countrycode << phonenumber[y] [0] ....
break;

default:
cout << "Phone number: ";
cout << phonenumber[y][0] .....
}

I thought I was? ...

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.

Sure. In general:

do
{
    [I]display menu
    input [B]MenuValue[/B]
    process the menu entry selected[/I]
} while ([b]MenuValue[/b] != ValueOfExitChoice);

Also, you badly need to read this. Most people will not be able to follow your code making it hard to get help.

1. The limit is 10..Make it 11,12,...,n
2. Avoiding goto..Use break (loop)..Or return..If any func that you want to call..Call it first then break or return..

  1. The limit is 10..Make it 11,12,...,n

What do you mean?

Oh and also for the variables in my code I forgot to add this...

string name[1000];
char phonenumber[100][100]; //100 phonenumbers max 100 digits long
int countrycode;//holds countrycode
char a[];//random string that phonenumber is compared to

char b[100][100];
int j;

Also, you badly need to read this Most people will not be able to follow your code making it hard to get help.

Can you give examples of what I can do to improve my writing?

//...
switch(i)
{
   case 11: //old 
   case 10: //new
   //...
}
//...

Thanks!!! Cikara
Fixed my program :)
Thanks again.

char a[];//random string that phonenumber is compared to

The variable a as defined can hold 0 characters. Therefore there is nothing to compare it to. If you try to load anything into it, you will overwrite some other variable or crash your program. Give a a size.

Can you give examples of what I can do to improve my writing?

Examples are in the link posted.

You can put all of that delay crap in a loop.

The variable [ICODE]a[/ICODE] as defined can hold 0 characters. Therefore there is nothing to compare it to. If you try to load anything into it, you will overwrite some other variable or crash your program. Give a a size.

Maybe you didn't see this:

char a[] = "1234567890";

Examples are in the link posted.

I meant cant like someone rewrite a few lines of my code in a "better" way.

You can put all of that delay crap in a loop.

What do you mean?
If you know a better way for the console to type "Thank you for using this program" in a slowly and then close...can you please tell me?

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.