Thread: Errors in Dev-C++ program

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Errors in Dev-C++ program

    Hi all, noob here. I’m trying to run a program in Dev-C++ that was created in Turbo C++. I already changed a few things, like updating the header files and replacing clrscr() with fflush, but it gives me a runtime error which causes the program to close (although the program is along, so I won’t post all of it here). There are also several compile errors, which I placed in comments on the line where they appear in the code:


    Does anyone know how to fix these? Here is the code for the function containing the errors:
    Code:
    void insertCar(){
        int valid1, valid2, valid3, valid;
        char carArray[MAX_LENGTH]={'\0'};
        char *honda = "honda", *ford = "ford", *mercedes = "mercedes", *toyota = "toyota";
        int i,p=0;  //[Warning]   matches this `i' under ISO standard rules
        int k;  //[Warning]   matches this `k' under ISO standard rules
        int numDigits;
        int indexOfFirstDigit=0;
        int yearAsInt;
        char *make;
        do{
    	valid = 1;
    	valid1 = 1;
    	valid2 = 1;
    	valid3 = 1;
    	char *year;
    	make = '\0';
    	yearAsInt=0;
    	numDigits=0;
    	i=0;
    	cout <<"\nEnter car information: ";
    	fflush(stdin);
    	gets(carArray);
    	fflush(stdin);
    
    	while (i<MAX_LENGTH){
    	    if (isdigit(carArray[i]))
    		numDigits++;  
    	    i++;
    
    	}
    
    	if (numDigits == YEAR_SHORT || numDigits == YEAR_LONG){
    	    i=0;
    	    while (i<MAX_LENGTH){
    		if (isdigit(carArray[i])){  
    		    indexOfFirstDigit=i;
    		    break;
    		}
    		i++;
    	    }
    	    strncpy(year, carArray+indexOfFirstDigit, numDigits);
    	    yearAsInt = atoi(year);    
    	   
    	    if(indexOfFirstDigit>0)
    	    {
    	    strncpy(make, carArray, indexOfFirstDigit-1);
    
    		   int f = 0;
    
    		for(int j = 0; j < strlen(make); j++)
    		if(make[j] == ':')
    		{
    		       f = 1;
    		       make[j] = '\0';
    		       break;
    		}
    		if(f==0)
    		  {
    			cout<<"colon is missing"<<endl;
    			insertCar();
    		  }		
    	    }
    
    	    else{
    	    i=0;
    	    while(carArray[i]!='\0'){
    	    p++;
    	    i++;
    	    }
    	    p = p-numDigits;
    	    strncpy(make, carArray+numDigits+1,p);
    	    }
    
    		for(int i = 0; i < strlen(make); i++)  //[Warning]   matches this `i' under old rules
    			if(make[i]>=65 && make[i]<=90)
    			make[i] = make[i] + 32;
    
    	    int f = 0;
    
    			for(i = 0; i < strlen(honda); i++)  //[Warning] name lookup of `i' changed
    
    			   {
    				if(make[i] != honda[i])
    				  {
    				  f = 0;
    				  break;
    				    }
    				else
    				   f = 1;
    			    }
    
    			   if(f==0)
    			   for(i = 0; i < strlen(ford); i++)
    			   {
    				if(make[i] != ford[i])
    				  {
    				    f = 0;
    				    break;
    				  }
    
    				else
    				   f = 1;
    			   }
    
    			   if(f==0)
    			   for(i = 0; i < strlen(toyota); i++)
    			   {
    				if(make[i] != toyota[i])
    				  {
    				  f = 0;
    				  break;
    				    }
    
    				else
    				   f = 1;
    			   }
    
    			   if(f==0)
    			   for(i = 0; i < strlen(mercedes); i++)
    			   {
    				if(make[i] != mercedes[i])
    				  {
    				  f = 0;
    				  break;
    				    }
    
    				else
    				   f = 1;
    			   }
    
    	    if(f==1)
    	    {
    	    if (numDigits == YEAR_SHORT){
    		if (yearAsInt >= 75 && yearAsInt <= 99 ||
    		    yearAsInt >= 0 && yearAsInt <= 2)
    		    valid1=1; 
    
    		else{
    		    valid1=0;
    		    cout<<endl<<"Invalid range";
    		}
    	    }
    	    else{   
    		if (yearAsInt >= 1975 && yearAsInt <= 2002)
    		    valid2=1;   
    		else{               
    		    valid2=0;
    		    cout<<endl<<"Invalid range";
    		}
    	    }
    
    	    if(numDigits == YEAR_SHORT && valid1 == 1){
    		if(yearAsInt >= 75 && yearAsInt <= 99)
    
    			yearAsInt = yearAsInt + 1900;
    
    		else if(yearAsInt >= 0 && yearAsInt <= 2)
    
    			yearAsInt = yearAsInt + 2000;
    	    }
    	  }
    
    	  else{
    		cout<<" make is not in the list"<<endl;
    		valid=0;
    	    }
    	  }
    	else{
    	    valid3=0;
    	    cout<<"wrong year format"<<endl;
    	}
        }
        while(!valid || !valid1 || !valid2 || !valid3);
    
        if(x < 4)
        {
    	carInfo[flag] = new char[strlen(carArray)];
    	makeName[flag] = new char[strlen(make)];
    
    	for(int j = 0; j < strlen(carArray); j++)
    		carInfo[x][j] = carArray[j];
    		carInfo[x][strlen(carArray)] = '\0';
    
    	 for(int k = 0; k < strlen(make); k++)  //[Warning]   matches this `k' under old rules
    		makeName[x][k] = make[k];
    		makeName[x][strlen(make)] = '\0';
    
    		yearArray[x] = yearAsInt;
    
    	int f = 0;
    	if(x>0 && yearArray[x - 1] == yearArray[x]){
    
    		for(k = 0; k < strlen(make); k++){  // [Warning] name lookup of `k' changed
    
    			if(makeName[x-1][k] == make[k])
    				f = 1;
    
    			else
    				break;
    			}
    
    		 if(f == 1){
    		 cout<<"Duplicate Entry";
    		 insertCar();
    		 }
    		}
        flag++;
        x++;
    
        cout << "\n******************************\n\n";
        cout << "Inserted\n\n";
        cout << "******************************\n\n";
        }
    
       else
       cout<<endl<<"Exceed the input limit";
       displayMenu();
    }
    Hope this isn’t too ugly...sorry it's so long. This is my first time posting here, and I tried not to appear as a big artard. Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > gets(carArray);
    > fflush(stdin);
    The FAQ explains why you should NEVER use these things, even if you seem to be having some success. They're dangerous and non-portable (respectively).

    > for(int i = 0; i < strlen(make); i++) //[Warning] matches this `i' under old rules
    In old C++, this meant

    int i;
    for( i = 0; i < strlen(make); i++) //[Warning] matches this `i' under old rules
    so there was an i in scope until the end of whatever block the loop was in.

    Now,
    for(int i = 0; i < strlen(make); i++) //[Warning] matches this `i' under old rules
    really does limit the scope of i to this loop.

    The solution is to not have so many declarations of i floating all over the place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Always try and avoid using too many declarations of the same variable. Also it is recommended to use "intrinsic documentation", this involves calling the variables something other than 'i' and 'f', to give a better idea to people reading your code, what the point of the variable is.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thats programmer style really. I use i j and k for my for loops. But, only if I am counting. When I made by Battleships game, and I was creating my ships with loops, I called the counter the ship name.

    Code:
    for ( int Ldestroyer = 0; Ldestroyer < 5; ++Ldestroyer )
    {
       //
    }
    Double Helix STL

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Gets is also very, very bad and can be a reason as to why it crashes.
    See the FAQ about that and how to remedy it.
    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
    Join Date
    Apr 2008
    Posts
    2
    Thanks everyone for your input! I appreciate the help. And thanks for not making this first-timer feel like a total dummy. =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Few Simple Program Errors
    By pobri19 in forum C Programming
    Replies: 3
    Last Post: 05-22-2008, 06:12 PM
  2. Replies: 4
    Last Post: 03-10-2008, 07:03 AM
  3. compile errors with date program
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 08-07-2005, 07:17 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 10:40 AM