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

This is a discussion on error: expected unqualified-id before "while" (???) within the C++ Programming forums, part of the General Programming Boards category; I got a very puzzling error message. I figured that the compiler might have thought that the semicolon after the ...

  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-03-2008 at 11:14 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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 10:37 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21