practice finding/correcting errors

I am trying to practice finding and correcting errors. I have a decent knowledge about C++ but not enough of course, which is why I am practicing.

- the end of a do while loop is where the error is... giving me an endless loop without stopping at some point in the output.
- I try to eliminate the "do" and simply just do the while loop. It did make a small correction but not perfected it. Instead it immediately stop or finish the program, however skip putting questions/input like phone numbers and answering Y/N question...
-I was trying to debug it but I don't see what is the actual problem.

P.S. I am running on Xcode if it makes any difference. I never experience running on windows compiler so I don't know if it works better there than Xcode.

I can't put the whole program on here because its over 9000 characters. but I'm putting the errors I have for two problems
1) while(----), getchar(), (code will never be executed)
2) length1/ length2 (Unused variable 'length1', 'length2')

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(choice==1||2||3||4||5||6);
    
    getchar();
    


void office :: nameconcate (void)
{
    int length1 = strlen(name1);
    int length2 = strlen(name2);
    
    strcpy(name,name1);
    strcat(name1,name2);
}
1
2
3
while(choice==1||2||3||4||5||6);
    
    getchar();


There are actually 2 problems in line 1. The first is the way that you check your condition. The || (or) token compares 2 expressions, so everything between the or's is treated as a complete expression. Because any non-zero value is treated as "true", your check is the same as
while(choice==1||true||true||true||true||true)

Because anything or'ed with "true" will evaluate to true, the while condition is always true. What you need to do instead is

while( choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 6)

Better yet, while (choice >= 1 && choice <= 6).

The second problem is that you put a semicolon at the end of the line. A semicolon is a valid (empty) expression. So, you are doing nothing in your loop.
1
2
3
4
5
6
7
8
void office :: nameconcate (void)
{
    int length1 = strlen(name1);
    int length2 = strlen(name2);
    
    strcpy(name,name1);
    strcat(name1,name2);
}


The reason your compiler said that length1 and length2 are unused is because you didn't use them for anything. This is probably a compiler warning, but why did you create the variables if you did not intend to use them?
thanks! i didn't think it was that picky with line 1,

I have a habit of using this technique of length1 and length2 when it comes to adding names to data input by users. But I see your point.
i didn't think it was that picky with line 1,


Compilers are nothing if not picky. You may find yourself repeating this line over and over through your programming days.
Topic archived. No new replies allowed.