Thread: Problems about 2 dimensional array

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Smile Problems about 2 dimensional array

    Here is my source code.I found some problems is that this program cannot store data completely.It juzt only can store 1st alphabet of the string that i had entered.I cant found the problems.So, can any1 plz help me check my code and tel me where is the problem.If got any places that i can improve my source code, plz let me noe.I wil very appreciate it.Thx!!!


    insert
    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    ofstream out;
    
    //class vehicle definition
    class vehicle
    {
    private:
            //data members declaration
            char Mname[5][15];
            char Mbrand[5][15];
            char Mprice[5][15];
            char Mfacname[5][15];
            char Mfacdate[5][15];
            char Mfachistory[5][15];
            char Ebrand[5][15];
            char horsepower[5][15];
            char Eprice[5][15];
            char size[5][15];
            char Wbrand[5][15];
            char Wprice[5][15];
            int Myear,Eyear,Wyear;
                    
    public:
            
           //definition of the functions
           void model(char newMname[][15],char newMprice[][15],int i)
           {
           out<<"Name of the model is "<<newMname[i][15]<<"."<<endl;
           out<<"Price of the model is Rm "<<newMprice[i][15]<<"."<<endl;
           }
           /////////////////////////////////////////////////////////////////////////////////////
           void model(char newMname[][15],char newMprice[][15],char newMbrand[][15],int i)
           {
           out<<"Name of the model is "<<newMname[i][15]<<"."<<endl;
           out<<"Brand of the model is "<<newMbrand[i][15]<<"."<<endl;
           out<<"Price of the model is Rm "<<newMprice[i][15]<<"."<<endl;
           }
           //////////////////////////////////////////////////////////////////////////////////////
           void manufacturer(char newMfacname[][15],char newMfacdate[][15],int i)
           {
           out<<"The name of the manufacturer is "<<newMfacname[i][15]<<"."<<endl;
           out<<"The manufactured date is "<<newMfacdate[i][15]<<"."<<endl;
           }
           //////////////////////////////////////////////////////////////////////////////////////
           void manufacturer(char newMfacname[][15],char newMfacdate[][15],char newMfachistory[][15],int i)
           {
           out<<"The name of the manufacturer is "<<newMfacname[i][15]<<"."<<endl;
           out<<"The manufactured date is "<<newMfacdate[i][15]<<"."<<endl;
           out<<"The manufacturer has "<<newMfachistory[i][15]<<" years experience in this field."<<endl;
           }
           //////////////////////////////////////////////////////////////////////////////////////
           void engine(char newEbrand[][15],char newhorsepower[][15],int i)
           {
           out<<"The brand of the engine is "<<newEbrand[i][15]<<"."<<endl;
           out<<"The horsepower of the engine is "<<newhorsepower[i][15]<<" cc."<<endl;
           }
           ///////////////////////////////////////////////////////////////////////////////////////
           void engine(char newEbrand[][15],char newhorsepower[][15],char newEprice[][15],int i)
           {
           out<<"The brand of the engine is "<<newEbrand[i][15]<<"."<<endl;
           out<<"The horsepower of the engine is "<<newhorsepower[i][15]<<" cc."<<endl;
           out<<"The price of the engine is Rm "<<newEprice[i][15]<<"."<<endl;
           }
           ///////////////////////////////////////////////////////////////////////////////////////
           void wheel(char newWbrand[][15],char newWprice[][15],int i)
           {
           out<<"The brand of the wheel is "<<newWbrand[i][15]<<"."<<endl;
           out<<"The price of the wheel is Rm "<<newWprice[i][15]<<"."<<endl;
           }
           ///////////////////////////////////////////////////////////////////////////////////////
           void wheel(char newsize[][15],char newWbrand[][15],char newWprice[][15],int i)
           {
           out<<"The size of the wheel is "<<newsize[i][15]<<" inches."<<endl;
           out<<"The brand of the wheel is "<<newWbrand[i][15]<<"."<<endl;
           out<<"The price of the wheel is Rm "<<newWprice[i][15]<<"."<<endl;
           };
           ///////////////////////////////////////////////////////////////////////////////////////
           void input()//function to input and store string into array accordingly
           {
           for(int i=0;i<5;i++)
           {
           cout<<"_________________________________________________________"<<endl;
           cout<<"          Enter the details of the vehicle no. "<<i+1<<endl;
           cout<<"---------------------------------------------------------"<<endl;
           cout<<"Please enter vehicle model: ";
           fflush(stdin);
           cin.getline(Mname[i],15);
           cout<<"Please enter the brand of the vehicle: ";
           cin.getline(Mbrand[i],15);
           cout<<"Please enter the price of the model: ";
           cin.getline(Mprice[i],15);
           cout<<"Please enter the name of the manufacturer: ";
           cin.getline(Mfacname[i],15);
           cout<<"Please enter the date of the vehicle manufactured: ";
           cin.getline(Mfacdate[i],15);
           cout<<"Please enter the experience of the manufacturer: ";
           cin.getline(Mfachistory[i],15);
           cout<<"Please enter the brand of the engine: ";
           cin.getline(Ebrand[i],15);
           cout<<"Please enter the horsepower of the engine: ";
           cin.getline(horsepower[i],15);
           cout<<"Please enter the price of the engine: ";
           cin.getline(Eprice[i],15);
           cout<<"Please enter the wheel size: ";
           cin.getline(size[i],15);
           cout<<"Please enter the brand of the wheel: ";
           cin.getline(Wbrand[i],15);
           cout<<"Please enter the price of the wheel: ";
           cin.getline(Wprice[i],15);
           cout<<endl;
           system("cls");
           };
           }
           /////////////////////////////////////////////////////////////////////////////////////////
           void output()//function to print out string stored in the array accordingly
           {
           int option;
           do
           {
           cout<<"============================================================="<<endl;
           cout<<"Choose 1 option to show the details of the vehicle from below"<<endl;
           cout<<"============================================================="<<endl;
           cout<<"(1)name and price of the model,"<<endl;
           cout<<"   name and the manufactured date,"<<endl;
           cout<<"   brand and horsepower of the engine,"<<endl;
           cout<<"   brand and price of the wheel."<<endl;
           cout<<"(2)name,brand and price of the model,"<<endl;
           cout<<"   name,the manufactured date and manufacturer experience,"<<endl;
           cout<<"   brand,horsepower and price of the engine,"<<endl;
           cout<<"   size,brand and price of the wheel."<<endl;
           cout<<"(3)name,brand and price of the model,"<<endl;
           cout<<"   name and the manufactured date,"<<endl;
           cout<<"   brand,horsepower and price of the engine,"<<endl;
           cout<<"   brand and price of the wheel."<<endl;
           cout<<"Option has been selected is ";
           cin>>option;
           cout<<endl;
           switch(option)
           {
           case 1:for(int i=0;i<5;i++)
                 {
                 out<<"________________________________________________"<<endl;
                 out<<"        Details of vehicle no."<<i+1<<" as below:"<<endl;
                 out<<"------------------------------------------------"<<endl;
                 model(Mname,Mprice,i);
                 manufacturer(Mfacname,Mfacdate,i);
                 engine(Ebrand,horsepower,i);
                 wheel(Wbrand,Wprice,i);
                 out<<endl;
                 };
                 break;
           case 2:for(int i=0;i<5;i++)
                 {
                 out<<"________________________________________________"<<endl;
                 out<<"        Details of vehicle no."<<i+1<<" as below:"<<endl;
                 out<<"------------------------------------------------"<<endl;
                 model(Mname,Mprice,Mbrand,i);
                 manufacturer(Mfacname,Mfacdate,Mfachistory,i);
                 engine(Ebrand,horsepower,Eprice,i);
                 wheel(size,Wbrand,Wprice,i);
                 out<<endl;
                 };
                 break;
           case 3:for(int i=0;i<5;i++)
                 {
                 out<<"________________________________________________"<<endl;
                 out<<"        Details of vehicle no."<<i+1<<" as below:"<<endl;
                 out<<"------------------------------------------------"<<endl;
                 model(Mname,Mprice,Mbrand,i);
                 manufacturer(Mfacname,Mfacdate,i);
                 engine(Ebrand,horsepower,Eprice,i);
                 wheel(Wbrand,Wprice,i);
                 out<<endl;
                 };
                 break;
           default:cout<<"Invalid option."<<endl;
                   cout<<"Please choose an option again."<<endl;
                   break;
           }
           system("cls");
           }while(option!=1&&option!=2&&option!=3);
           }
           ///////////////////////////////////////////////////////////////////////
           void check_model(int year)
           {
           if(year<1980)    
           out<<"This model is extremely outdated and can be assumed that it is not exist anymore."<<endl;
           if(year>1980&&year<2001)
           out<<"This model is very old."<<endl;
           if(year>2000&&year<2009)
           out<<"This model is new and can be seen everywhere."<<endl;
           if(year>2008)
           out<<"This car not yet exist."<<endl;
           }
           ////////////////////////////////////////////////////////////////////////
           void check_engine (int year) 
           {
           if(year>1995&&year<2009)
           out<<"The car is safe to drive on road."<<endl;
           else if(year>1900&&year<1996)
           out<<"This car may have many problem and unlawful to drive it on road."<<endl;
           else
           out<<"The year of manufacturing is not valid. Please check your engine again."<<endl;
           }
           /////////////////////////////////////////////////////////////////////////
           void check_wheel(int year)
           {
           if(year<1995)
           out<<"The wheel should be changed as it might be broken anytime."<<endl;
           if(year>1995&&year<2009)
           out<<"The wheel is in good condition and it is safe for driving long distance."<<endl;
           if(year>2009)
           out<<"The wheel not yet exist."<<endl;
           }
           /////////////////////////////////////////////////////////////////////////
           void checking_vehicle()
           {
           cout<<"*********************************************************"<<endl;
           cout<<"              CHECKING THE VEHICLE CONDITION             "<<endl;
           cout<<"*********************************************************"<<endl;
           for(int i=1;i<=5;i++)
           {
           out<<"#############"<<endl;
           out<<"VEHICLE NO. "<<i<<" "<<endl;
           out<<"#############"<<endl;
           cout<<"Please enter the year of the model of vehicle no. "<<i<<" :";
           cin>>Myear;
           check_model(Myear);
           out<<endl;
           cout<<"Please enter the year of the engine of vehicle no. "<<i<<" :";
           cin>>Eyear;
           check_engine(Eyear);
           out<<endl;
           cout<<"Please enter the year of the wheel of the vehicle no. "<<i<<" :";
           cin>>Wyear;
           check_wheel(Wyear);
           out<<endl;
           cout<<endl;
           system("cls");
           };
           }
    
    
    };
          
    
    
    
    #include<iostream>
    #include<cstring>
    #include<fstream>
    #include"vehicle.h"
    //function for input and output
    
    using namespace std;
    
    int main()
    {
    int x;
    char Z;
    vehicle car;//create object car
    vehicle motobike;//create object motobike
    
    out.open("output.txt");
    do
    {
    cout<<"====================================================="<<endl;
    cout<<"Details of 5 different cars and 5 different motobikes"<<endl;
    cout<<"====================================================="<<endl;
    
    do
    {
    cout<<"Choose 1 option from below:"<<endl;
    cout<<"(1)car"<<endl;
    cout<<"(2)motobike"<<endl;
    cout<<"(3)quit"<<endl;
    cout<<endl;
    cout<<"Option chose by the user is ";
    cin>>x;
    cout<<endl;
    
    switch(x)
    {
    case 1:cout<<"Key-in the inputs for 5 different cars as below:"<<endl;
           car.input();
           car.output();
           car.checking_vehicle();
           break;
    
    case 2:cout<<"Key-in the inputs for 5 different motobikes as below:"<<endl;
           motobike.input();
           motobike.output();
           motobike.checking_vehicle();
           break;
    
    case 3:return 0;
    
    default:cout<<"Invalid option."<<endl;
            cout<<"Please choose an option again."<<endl;
            cout<<endl;
            break;
            
    }
    }while(x!=1&&x!=2&&x!=3);
    
    cout<<"Press Y or y to continue: ";
    cin>>Z;
    system("cls");
    }while(Z=='Y'||Z=='y');//ask users permission whether to continue the program
    
    system("pause");
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    if you have no idea where the error is, i doubt anyone will go through all of your code to find it for you. you should go through it thoroughly and narrow down what functions might be the problem and highlight them in red or something. also, putting all those lines of comments doesnt make it easier to read, but harder.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know about storing strings, but I note you're only ever printing one letter: whatever[i][15] is one single character spot. If you want to print out the entire string, perhaps printing out whatever[i] will do better.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it would be a good thing to try and narrow it down.
    Might I also suggested fixing up some indentation? It will make the code much easier to read.
    http://cpwiki.sf.net/User:Elysia/Indentation
    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. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  2. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 05:27 AM
  3. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-18-2005, 11:49 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM