Thread: error: expected unqualified-id before "while" (???)

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    15

    error: expected unqualified-id before "while" (???)

    I got a very puzzling error message. I figured that the compiler might have thought that the semicolon after the void prompt (void) was a mistake and not an indication of a prototype however, even with that line commented out the error persisted (along with subsequent errors produced by the lack of that prototype). The code potentially relevant to the problem is right below.

    Code:
    char ints[6];
    char floats[20];
    
    void prompt (void);
    
    class data
    {
    public:
    	char last[31], first[36];
    	float salary;
    	int yrhired;
    	void display(void);
    	void modify(void);
    }employee[20];
    
    int count = 0;
    
    while (count < 20){
    	employee[count].last[0] = '\0';
    	employee[count].first[0] = '\0';
    	employee[count].salary = -1;
    	employee[count].yrhired = -1;
    	count++;
    }
    Last edited by Mikal; 12-04-2008 at 12:14 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't just do things; every piece of executable code must appear inside a function of some kind.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    I'm not sure what you mean but this particular section of the code is inside of a header file that I made. I didn't post the entire code because it's fairly long. I just posted the code around the line with the error.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I guess it's ok to post all your code so that the people around would be able to understand and see what's the problem with the code. Because the code that you posted right there was assumed that the code is not in a function of some sort.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    15
    my_library.h
    Code:
    char ints[6];
    char floats[20];
    
    void prompt(void);
    
    class data
    {
    public:
    	char last[31], first[36];
    	float salary;
    	int yrhired;
    	void display(void);
    	void modify(void);
    }employee[20];
    
    int count = 0;
    
    while (count < 20){
    	employee[count].last[0] = '\0';
    	employee[count].first[0] = '\0';
    	employee[count].salary = -1;
    	employee[count].yrhired = -1;
    	count++;
    }
    
    void display_choose(void){
    	int display_choice;
    	cout << "Which record? (1-20)" << endl;
    	cin >> display_choice;
    	if(display_choice > 20 || display_choice < 1){
    		cout << "Invalid choice" << endl;
    		display_choose();
    	}
    	else
    		employee[display_choice+1].display();
    }
    
    void data::display(void){
    	cout << endl << last << "," << first << endl << "Salary: " << salary << endl << "Year hired: " << yrhired << endl;
    	prompt();
    }
    
    void modify_choose(void){
    	int modify_choice;
    	cout << "Which record? (1-20)" << endl;
    	cin >> modify_choice;
    	if (modify_choice > 20 || modify_choice < 1){
    		cout << "Invalid choice" << endl;
    		modify_choose();
    	}
    	else
    		employee[modify_choice+1].modify();
    }
    
    void data::modify(void){
    	int modcount = 1;
    	cout << endl << "Last name: ";
    	last[0] = getchar();
    	while (modcount < 30 && last[modcount-1] != '\n'){
    		last[modcount] = getchar();
    		modcount++;
    	}
    	last[modcount] = '\0';
    
    	modcount = 1;
    	cout << endl << "First and Middle: ";
    	first[0] = getchar();
    	while (modcount < 35 && last[modcount-1] != '\n'){
    		first[modcount] = getchar();
    		modcount++;
    	}
    	first[modcount] = '\0';
    
    	cout << endl << "Salary: ";
    	cin >> salary;
    	
    	cout << endl << "Year hired: ";
    	cin >> yrhired;
    
    	prompt();
    }
    
    void save(void){
    	int savecount = 0;
    	FILE *fp;
    	char endline[2];
    	endline[0] = '\n';
    	endline[1] = '\r';
    	fp = fopen("company.db", "wb");
    		if (fp = NULL){
    			cout << "Error opening 'company.db'";
    			prompt();
    		}
    	else{
    	while (savecount < 20){
    		fwrite("Last: ", 1, 6, fp);
    		fwrite(employee[savecount].last, 1, 30, fp);
    		fwrite(endline, 1, 2, fp);
    		fwrite("First and Middle: ", 1, 18, fp);
    		fwrite(employee[savecount].first, 1, 35, fp);
    		fwrite(endline, 1, 2, fp);
    		fwrite("Salary: ", 1, 8, fp);
    		sprintf(floats, "&#37;f", employee[savecount].salary);
    		fwrite(floats, 1, 20, fp);
    		fwrite(endline, 1, 2, fp);
    		fwrite("Year hired: ", 1, 12, fp);
    		sprintf(ints, "%d", employee[savecount].yrhired);
    		fwrite(ints, 1, 6, fp);
    		fwrite(endline, 1, 2, fp);
    		savecount++;
    	}
    	}
    	fclose (fp);
    	prompt();
    }
    
    void retrieve(void){
    	char character;
    	FILE *fp;
    	fp = fopen("company.db", "r");
    		if (fp = NULL){
    			cout << "Error opening 'company.db'";
    			prompt();
    		}
    	else{
    	while ((character = fgetc(fp)) != EOF){
    		printf("%c", character);
    	}
    	}
    	fclose(fp);
    	prompt();
    }
    my_program.cpp
    Code:
    #include <iostream>
    using namespace std;
    #include "my_library.h"
    
    void prompt(void){
    	char choice;
    	printf("(1) Display\n(2) Modify\n(3) Save\n(4) Retrieve\n(5) Exit\n");
    	cin >> choice;
    	switch(choice){
    		case 1:
    			display_choose();
    			break;
    		case 2:
    			modify_choose();
    			break;
    		case 3:
    			save();
    			break;
    		case 4:
    			retrieve();
    			break;
    		case 5:
    			exit(0);
    			break;
    		default:
    			printf("Invalid choice\n");
    			prompt();
    			break;
    	}
    }
    
    int main() {
    	prompt();
    }
    I don't quite see how the rest of the code is relevant to the problem, but beggars can't be choosers.
    Last edited by Mikal; 12-03-2008 at 11:37 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mikal
    I'm not sure what you mean but this particular section of the code is inside of a header file that I made. I didn't post the entire code because it's fairly long. I just posted the code around the line with the error.
    Post the smallest and simplest code that demonstrates the error. If this really is your code, then what tabstop means is that the problem could be because you have a while loop hanging in the middle of nowhere. It should be in a function.

    EDIT:
    Yes, tabstop's diagnosis is correct, in my opinion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mikal View Post
    my_library.h
    Code:
    char ints[6];
    char floats[20];
    
    void prompt(void);
    
    class data
    {
    public:
    	char last[31], first[36];
    	float salary;
    	int yrhired;
    	void display(void);
    	void modify(void);
    }employee[20];
    
    int count = 0;
    
    while (count < 20){
    	employee[count].last[0] = '\0';
    	employee[count].first[0] = '\0';
    	employee[count].salary = -1;
    	employee[count].yrhired = -1;
    	count++;
    }
    
    // ...
    I don't quite see how the rest of the code is relevant to the problem, but beggars can't be choosers.
    While loop outside function...
    Your indentation is suffering at some places, as well.
    And a lot of that code does not belong in a header. It should be moved inside the .cpp file. ONLY declarations and inline functions should be placed in headers. No non-inline functions, no variables and absolutely no while-loops outside headers.
    And you can also get rid of the "void" in the parameter lists. They are unnecessary and do nothing at all, but take space.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unqualified name in template
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2008, 03:48 AM
  2. qualified name and unqualified name
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2008, 08:41 AM
  3. "while" question
    By elrookie in forum C Programming
    Replies: 13
    Last Post: 06-29-2007, 05:09 PM
  4. "for" loop or "while" loop?
    By s_ny33 in forum C++ Programming
    Replies: 11
    Last Post: 04-24-2005, 01:28 PM