In need of file/search help

This is a discussion on In need of file/search help within the C++ Programming forums, part of the General Programming Boards category; Ok, in the program I am making (code is below), I am having a problem with my file and searching ...

  1. #1
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79

    In need of file/search help

    Ok, in the program I am making (code is below), I am having a problem with my file and searching code. after I enter my information, it is sent to a file. I then do a searh with the information I entered for a specific word, and it brings up all the information I entered in its struct. My problem is this. When the information is searched and printed out, 0048186c is printed out right before the information in my model field. Please help.

    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    main()
    {	
    	int choice, a=0, b=0;
    	int c=874;
    	char make[30], model[30], color[32], file[20]; 
    	int door;
    	char suck[90876];
    	struct automobile
    	{
    		char make[20];
    		char model[20];
    		char color[20];
    		int door;
    		char suck[32];
    	};
    	automobile mycar[100];
    	ofstream outfile;
    	do
    		{	
    			cout<<"enter the name of the file you want to work with";
    			cin.get(file, 20);
    			cin.ignore(80, '\n');
    			outfile.open(file, ios::app);
    			if(outfile)
    			{
    			cout<<"Plese enter the number of doors. Press 874 to quit";
    			cin>>mycar[a].door;
    			cin.ignore(100, '\n');
    			while(mycar[a].door!=c)
    				{
    					cout<<"What is the make of the car?";
    					cin.get(mycar[a].make, 20);
    					cin.ignore(100, '\n');
    					outfile<<mycar[a].make<<endl;
    					cout<<"What is the model of the car?";
    					cin.get(mycar[a].model, 20);
    					cin.ignore(100, '\n');
    					outfile<<mycar[a].model<<endl;
    					cout<<"What color is the car?";
    					cin.get(mycar[a].color, 20);
    					cin.ignore(100, '\n');
    					outfile<<mycar[a].color<<endl;
    					cout<<"How much does this car suck?";
    					cin.get(mycar[a].suck, 32);
    					cin.ignore(100, '\n');
    					outfile<<mycar[a].suck<<endl;
    					a=a+1;
    					cout<<"How many doors does this car have?";
    					cin>>mycar[a].door;
    					cin.ignore(100, '\n');
    					outfile<<mycar[a].door<<endl;
    				}
    				outfile.close();
    				a=a-1;
    				for(b=0; b<=a; b++)
    				{
    					cout<<mycar[a].make<<endl<<mycar[a].model<<endl<<mycar[a].color<<endl<<mycar[a].suck<<endl<<mycar[a].door<<endl;
    				}
    				cout<<"What do you want to search for?\n1 - make 2 - model 3 - color 4 - suckiness 5 - doors";
    				cin>>choice;
    				switch(choice)
    				{
    				case 1:
    				
    					cout<<"What make do you want to look for?";
    					cin.get(make[30]);
    					cin.ignore(100, '\n');
    					for(b=0; b<=a; b++)
    					{
    						if (strcmp(mycar[b].make, make)==0)
    							{
    								cout<<mycar[a].make<<endl<<cout<<mycar[a].model<<endl<<mycar[a].color<<endl<<mycar[a].suck<<endl<<mycar[a].door<<endl;
    							}
    						break;
    					}
    					
    				case 2:
    				
    					cout<<"What  model do you want to look for?";
    					cin.get(model[30]);
    					cin.ignore(100, '\n');
    					for(b=0; b<=a; b++)
    					{
    						if (strcmp(mycar[b].model, model)==0)
    						{
    							cout<<mycar[a].make<<endl<<cout<<mycar[a].model<<endl<<mycar[a].color<<endl<<mycar[a].suck<<endl<<mycar[a].door<<endl;
    						}
    						break;
    					}
    				
    				case 3:
    				
    					cout<<"What color do you want to look for?";
    					cin.get(color[32]);
    					cin.ignore(100, '\n');
    					for(b=0; b<=a; b++)
    					{
    						if (strcmp(mycar[b].color, color)==0)
    						{
    							cout<<mycar[a].make<<endl<<cout<<mycar[a].model<<endl<<mycar[a].color<<endl<<mycar[a].suck<<endl<<mycar[a].door<<endl;
    						}
    						break;
    					}
    				
    				case 4:
    				
    					cout<<"What suckiness do you want to look for?";
    					cin.get(suck[90876]);
    					cin.ignore(100, '\n');
    					for(b=0; b<=a; b++)
    					{
    						if (strcmp(mycar[b].suck, suck)==0)
    						{
    							cout<<mycar[a].make<<endl<<cout<<mycar[a].model<<endl<<mycar[a].color<<endl<<mycar[a].suck<<endl<<mycar[a].door<<endl;
    						}
    						break;
    					}
    				
    				case 5:
    				
    					cout<<"How many doors do you want to look for?";
    					cin>>door;
    					cin.ignore(100, '\n');
    					for(b=0; b<=a; b++)
    					{
    						if (mycar[b].door==door)
    						{
    							cout<<mycar[a].make<<endl<<cout<<mycar[a].model<<endl<<mycar[a].color<<endl<<mycar[a].suck<<endl<<mycar[a].door<<endl;
    						}
    						break;
    					}
    				}
    				
    			}
    				else
    				{
    					cout<<"there was an error with the file you requested";
    				}
    			}while(mycar[a].door!=874);
    	
    		return 0;
    }
    This has been a public service announcement from GOD.

    111 1111

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >> cin.get(xxxxx[30]);

    change above to:

    cin.get(xxxxx, 30);

    in all the appropriate places and see if it helps.

    The type of garbage you are getting is often do to overreading arrays or pointer problems. In this case I think it is an overread problem as you are trying to read data into the 30th element of an array which doesn't exist (model[29] is the last element of the make array and the model array, etc.). Then when you try to print it to the screen you get garbage out.

Popular pages Recent additions subscribe to a feed

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