Thread: Windows Compiler gives error while Linux doesn't?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    Windows Compiler gives error while Linux doesn't?

    Hello,
    I coded a project for class on my openSuse machine. It worked flawless. When I turned it in to my teacher, she compiled it on Visual Studio and it gave her this error:
    Run-time Check Failure #3 - The variable userinput is being used without being initialized.

    Needless to say I got a zero on a project that worked flawlessly on my box. Is anyone able to spot this said problem with 'userinput' or is there some sort of difference between the linux compiler and the windows compiler that would make this happen? I would really appreciate any feedback. I will paste the entire project in.

    Code:
    # include <iostream>
    # include <limits> // INCLUDED FOR THE GETINT FUNCTION
    using namespace std;
    
    typedef int* IntArPtr;
    int labamount = 4; // Defines the amount of labs, globally set to 4.
    
    int pressenter() //I PUT THIS BABY IN THERE SO I DON'T HAVE TO TYPE THIS EVERYTIME I WANT THE USER TO PRESS ENTER.
    	{
    	cin.ignore(256, '\n');
    	cout << endl << "Press enter to continue...";
    	cin.get();
    	return 0;
    	}
    int getInt() // THIS IS NOT MY FUNCTION BUT I INCORPORATED IT TO SANITIZE INTEGER INPUT. OTHERWISE, IF THE USER
                  //ENTERS CHARACTERS TO AN INT CIN IT CAUSES SERIOUS PROBLEMS.
    	      // It isn't necessary for the program but the fact that entering a letter caused the program to start
    	     //looping annoyed me enough to find a solution to it.
    	{ 
    	int x = 0;
    		while(!(cin >> x))
    		{
    		cin.clear();
    		cin.ignore(numeric_limits<streamsize>::max(),'\n');
    		cout << "Invalid input..." << endl 
    		<< "It must be in this format: <ID> <LAB> <STATION>." << endl 
    		<< "(e.g. 12345 3 4 logs user 12345 input Lab 3, Station 4.)" << endl << ": ";;
    		}
    	return x;
    	}
    
    int login (IntArPtr db_input[],int labsize_input[]) //Login function!
    	{
    	int countcheck = 0,idcount;
    	int id,lab,station;
    	cout << "Enter your User id, lab, and station to login to." << endl 
    	<< "It must be in this format: <ID> <LAB> <STATION>." << endl 
    	<< "(e.g. 12345 3 4 logs user 12345 input Lab 3, Station 4.)" << endl << ": ";
    	id =  getInt();
    	lab = getInt();
    	station = getInt();
    	idcount = id;
    		while( idcount > 0) //This device divides the user id by 10 until there is no number divisible by 10 left.
    			    // It then makes sure it was able to count 5 times so that the user id is 5 digits.
    		{
    		countcheck++;
    		idcount = idcount/10;
    		}
    
    		if ((countcheck != 5) || (id < 0))
    		{
    		cout << "Your ID must be five numbers long!!!" << endl;
    		pressenter();
    		}
    		else if ((lab > labamount) || (lab < 1))
    		{
    		cout << "No such lab!" << endl;
    		pressenter();	
    		}
    		else if ((station > labsize_input[lab - 1]) || (station < 1))
    		{
    		cout << "Lab " << lab << " does not have a station " << station << "!" << endl; 
    		pressenter();
    		}	
    		else if (db_input[lab - 1][station - 1] != 0)	
    		{
    		cout << "That station is currently in use!" << endl;
    		pressenter();
    		}	
    		else
    		{
    		db_input[lab - 1][station - 1] = id;
    		cout <<"You have successfully logged in to Lab " << lab << ", station " 
    		<< station << " as user " << id << "!"; 
    		pressenter();	
    		}
    	return 0;
    	}
    int logoff (IntArPtr db_input[],int labsize_input[]) // Logoff function!
    	{
    	
    	int id,lab,station;
    	cout << "Enter your User id, lab, and station to logout of." << endl 
    	<< "It must be in this format: <ID> <LAB> <STATION>." << endl 
    	<< "(e.g. 12345 3 4 logs user 12345 input Lab 3, Station 4.)" << endl << ": ";
    	id =  getInt();
    	lab = getInt();
    	station = getInt();
    		if ((lab > labamount) || (lab < 1))
    		{
    		cout << "No such lab!" << endl;
    		pressenter();	
    		}
    		else if ((station > labsize_input[lab - 1]) || (station < 1))
    		{
    		cout << "Lab " << lab << " does not have a station " << station << "!" << endl; 
    		pressenter();
    		}	
    		else if (db_input[lab - 1][station - 1] != id)
    		{
    		cout << "There is no such user currently logged on to that workstation.";
    		pressenter();	
    		}
    		else if (db_input[lab - 1][station - 1] == id)
    		{
    		cout << "User " << id << " successfully logged out of Lab " << lab << " Station " << station << ".";
    		db_input[lab - 1][station - 1] = 0;
    		pressenter();
    		}
    	return 0;
    	}
    int displaylabs (IntArPtr db_input[],int labsize_input[]) // Display all workstations function!
    	{
    	cout << "Lab Number    Computer Stations" << endl;
    		for (int index1 = 0;index1 < labamount;index1++)
    		{
    		cout << index1 + 1 << "             "; 
    			for (int index2 = 0;index2 < labsize_input[index1];index2++)
    			{
    			cout << index2 + 1 << ": ";
    				if (db_input[index1][index2] == 0)
    				{
    				cout << "empty ";
    				}
    				else
    				{
    				cout << db_input[index1][index2] << " ";
    				}
    				
    			}
    		cout << endl;
    		}
    	pressenter();
    	return 0;
    	}
    int search (IntArPtr db_input[],int labsize_input[]) //Function to search for a user!
    	{
    	
    	int id;
    	bool searchresult = 0;
    	cout << "Please enter a user id:";
    	id =  getInt();
    		for (int index1 = 0;index1 < labamount;index1++)
    		{
    			for (int index2 = 0;index2 < labsize_input[index1];index2++)
    			{
    				if (db_input[index1][index2] == id)
    				{		
    				cout << "User: " << id <<" is currently logged into Lab " << index1 + 1 
    				<< " Station " << index2 + 1 << "." << endl;
    				searchresult = 1; 
    				}
    			}
    		}
    		if (searchresult == 0)
    		{
    		cout << "No such user id is presently logged in to any workstation.";
    		}
    		pressenter();
    	return 0;
    	}
    int menu (IntArPtr db_input[],int labsize_input[]) // Main menu which calls the rest of the functions.
    	{
    	char userinput;
    		while ((userinput != 'q') && (userinput != 'Q'))
    		{
    		userinput = '0';
    		cout << "Welcome to Lab X network." << endl << "Please choose an option:" << endl;
    		cout << "L - Login" << endl;
    		cout << "X - Logoff" << endl;
    		cout << "D - Display status of all workstations" << endl;
    		cout << "S - Search for a user logged in" << endl;
    		cout << "Q - quit" << endl << "admin@labx: ";
    		cin >> userinput;
    			if ((userinput == 'l') || (userinput == 'L'))
    			{
    			login(db_input,labsize_input);
    			}
    			else if ((userinput == 'x') || (userinput == 'X'))
    			{
    			logoff(db_input,labsize_input);
    			}
    			else if ((userinput == 'd') || (userinput == 'D'))
    			{
    			displaylabs(db_input,labsize_input);
    			}
    			else if ((userinput == 's') || (userinput == 'S'))
    			{
    			search(db_input,labsize_input);
    			}
    		}
    		for (int index1 = 0;index1 < labamount;index1++)
    		{
    		delete[] db_input[index1]; //Kill each array within the db.
    		}
    	delete[] db_input; // Kill the db.
    	return 0;
    	}
    
    int main ()
    	{
    	int labsize [] = {5,6,4,3}; //Defines the size of each lab here
    	IntArPtr *db = new IntArPtr[labamount]; //Create the lab array!
    		for (int index1 = 0;index1 < labamount;index1++)
    		{
    		db[index1] = new int[labsize[index1]]; //Create a dynamic array for each of the four labs!
    			for (int index2 = 0;index2 < labsize[index1];index2++)
    			{
    			db[index1][index2] = 0; //initializes each workstation to 0 which is understood by my program  
                                                 // to mean empty.
    			}
    		}
    	menu(db, labsize); //starts the menu, which controls when to run the functions for each menu option.
    	return 0;
    	}
    Thank you so much!
    David Taylor

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    	char userinput;
    		while ((userinput != 'q') && (userinput != 'Q'))
    		{
    Notice that userinput is declared, and you check (userinput != 'q') BEFORE it's assigned a value. This only works because you were lucky enough that the garbage value userinput contained was not equal to 'q'.

    You need to test on the same system you're going to be graded on. It's not illegal in standard C++ to use an ininitialized variable, but no compiler implements 100% of the standard and no two compilers are exactly alike.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    There actually is a very valuable lesson in receiving your zero grade: compile with warnings turned all the way up ( g++ -Wall -pedantic ) and pay attention to the warnings that are issued.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Neonblack and rags_to_riches, thank you so much for this! Finally it makes sense, I'm a little slow and I did deserve the zero at the end of the day *sigh*.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Cheer up. It's not the end of the world, is it? You you got smarter and won't do the same mistake next time.
    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.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    When I was a freshman in college and taking intro to C, I got a 0 on one of my assignments because I used two // line comments. These are allowed by gcc by default, but not by gcc -ansi, which the instructor used to compile out assignments.

    It sucks, and I was ........ed, but this is a mistake you only make once.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I don't think it deserved a zero. It's not like you didn't test it. You just had a bug that didn't reveal itself right away.

    If it were me I'd have given you 30 minutes to correct it and resubmit with no penalty. Harsh, man.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    While I agree that on its face it's harsh, I find that in these days of rampant grade inflation it's at the same time fairly refreshing. If it was laid out in the rules that any homework assignment must compile with no errors on Visual Studio 200x or absolutely no credit would be awarded, and the turned-in assignment did not fulfill that requirement, then it is fair (providing of course the school has somehow provided availability of the VS 200x compiler). If this rule was sprung upon the OP without warning, then it is definitely unfair and worthy of appeal to higher authorities.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Erm...so what DOES Linux do that Windows doesn't?
    By 7smurfs in forum Tech Board
    Replies: 19
    Last Post: 09-09-2005, 03:04 PM
  2. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  3. dual boot setup with windows and linux
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-14-2003, 07:37 AM
  4. Linux / Windows
    By MethodMan in forum Linux Programming
    Replies: 19
    Last Post: 07-09-2002, 02:58 PM
  5. Linux vs. windows
    By MicrosoftRep in forum Linux Programming
    Replies: 1
    Last Post: 03-20-2002, 02:42 PM