This is my program and when i run it its works however the part when it says press Q or R isn't working. When i press R is just freezes n does nothing. Q works though.
Lab 1
//Constants
#define STARS "\t**************************************\n"
#define BOSS "\tKenneth\n"
#define PROTO "\tInternet Protocol Address Program\n"
#define EQUAL "\t=================================\n"
#define SLASH "\t//////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"
#define SLASH2 "\t\\\\\\\\\\\\\\\\\\\\\\\\\\\//////////////\n"
#define CONT "\tPress any key to continue...\n\n"
#define PERC "\t%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
//Function List
void output1 (void);
void output2 (void);
char getclass (const int ip);
void Menu (void);
void output3 (void);
void output4 (void);
void output5 (void);
//void output44 (void);
char ipname1[25];
char ipname2[25];
char ipname3[25];
char IPClassA;
char IPClassB;
char IPClassC;
int IP1[4];
int IP2[4];
int IP3[4];
//int sum1,sum2,sum3;
//float mean1,mean2,mean3;
void output1 (void)
{
system("cls");
printf("\tInternet Protocol Address Program\n");
printf("\tProgrammer Kenneth\n");
printf("\tStage 1.0\n");
printf(STARS);
printf("\tThis program records and analyses \n\tIP Address details entered by the user\n");
printf(STARS);
printf(CONT);
getch();
}
void output2 (void)
{
int ipcheck;
int loopredo;
loopredo = 1;
system("cls");
printf(PROTO);
printf(EQUAL);
do
{
ipcheck=0;
while (ipcheck == 0)
{
printf("Enter Ip Name:");
scanf("%s",ipname1);
printf("Input IP Address:");
scanf("%d.%d.%d.%d",&IP1[0],&IP1[1],&IP1[2],&IP1[3]);
yeh i know it works fine but after input ip and ip name
it will say press Q or R
when i press R it just freezes and doesnt repeat the process
i want to repeat it with the loops i added into the script
er... if u look at them closely they are differnt i need help with both of them
Well, you need to put better titles for your threads. ERROR or HELP!!! URGENT aren't good, put something more descriptive to attract the attention you need.
Now some style points:
If you read some of my other posts, you will see that I HATE do while loops!!
They can almost always be written as a while or for loop.
The only real time you need a do-while loop is when you have to test something that can only be set at the end of the body of the loop. This is fairly rare.
1 2 3 4 5 6 7
bool MyTest;
do {
// do a whole lot of stuff
MyTest = true; //this has to go here for a valid reason
} while(MyTest==true);
i got my friend to help me
but i think the loops he did was to check if it works
and if it did it proceeds to the next step otherwise display error
is there a better way other then writing loop = 0 loop = 1?
while(test-expression) {
//this code is executed if test-expression is true
}
So it's easy to change a do-while into a while, they are very similar.
i
s there a better way other then writing loop = 0 loop = 1?
1 2 3 4 5 6 7 8 9 10 11 12 13
bool MenuLoop = true; //declaration and initialisation
MenuLoop = false; //assignment in your code
if(MenuLoop == true) //testing it
{
//do this code if true
}
else
{
//do this code if false
}
do{}while(); is fine if you are wanting the code to execute at least once before testing the condition. The only difference between do{}while(); and while(){} is that the first guarantees the loop will execute once before testing the condition where the other will test the condition first and then run the loop if the condition is true. I think it depends on program demands and personal preference as I know some programmers that do their code just so that they have do while loops instead of while loops.
The only difference between do{}while(); and while(){} is that the first guarantees the loop will execute once before testing the condition where the other will test the condition first and then run the loop if the condition is true.
That's right, but one can nearly always still write it as a while loop that will execute at least once.
It's not so bad if the body of the loop is a function call, but when the while condition is down 50 lines, it can cause confusion. Having lots of code in a loop is bad style in it's self.
The other thing that has happened on this forum ( read the Loops thread by SGM) is this:
A post was made that looked like this:
1 2
while (test expression) ;
It caused some confusion and debate, but it turned out that it was the while expression of the do while ! The while expression wasn't on the same line as the closing brace of the do, which made it look like a while with a null statement !!!
When I said that do-whiles are rarely necessary, I remember that came from Kernighan & Ritchie C programming.
The other thing is that novice coders might write a do while, out of habit, then discover they have problems with the logic, with the solution being they needed a while loop, that may not execute at all.
It's interesting to read some of the other posts about this, sometimes the advice given isn't right.
Yeah, Bjarne Stroustrup even says that he avoids do while loops. He only mentions it on two pages in The C++ Programming Language Special Edition (114, and 137). This is his excerpt from page 137:
Bjarne Stroustrup wrote:
In my experience, the do-statement is a source of errors and confusion. The reason is that its body is always executed once before the condition is evaluated. However, for the body to work correctly, something very much like the condition must hold even the first time through. More often then a would have guessed, I have found that condition not to hold as expected either when the program was first written and tested or later after the code preceding it has been modified. I also prefer the condition "up front where I can see it." Consequently, I tend to avoid do-statements.
I use do while loops once in a while just for my goof apps, but if I'm doing something serious I use while loops.
You use a do-while loop when it makes sense and that does not depend on the type of application you're making. do-while loops are very rarely needed, but it is pointless to use a different loop "just because".
That's right, but one can nearly always still write it as a while loop that will execute at least once.
You can almost always write it using goto as well, but that does not automatically mean you should do it.
Yeah, Bjarne Stroustrup even says that he avoids do while loops.
In my experience, the do-statement is a source of errors and confusion.
I also prefer the condition "up front where I can see it." Consequently, I tend to avoid do-statements.
Well I think I would go with Bjarne Stroustrup's observations, yes?
but it is pointless to use a different loop "just because".
I was trying to point people towards good programming practice, it's not "just because", because of the reasons I mentioned above and elsewhere on this forum. Obviously goto's are very bad practice, but for and while loops are good practice.