Desperately need help. have little time :(
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Desperately need help. have little time :(

  1. #1
    Join Date
    May 2014
    Posts
    1

    Desperately need help. have little time :(

    Can someone please help me out here. I've got an assignment to finish up on C++ and it just doesn't run properly. its a care hiring program that asks for details and prints it out at the end. when i enter my house number, address, and phone number it just goes loco. Help will be greatly appreciated. here's the code:

    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <cmath>
    void line ();
    using namespace std;

    // Declarations
    float price;
    float cost;
    char choice;
    char option;
    int numdays;



    // Function for car choice
    void carchoice ()
    {
    do { // Start of loop

    cout << "\n\t\t\t\tCARS FOR HIRE\n\n\n\tProduct Code\t\tItem Description\tCost Per Day\n";
    line ();
    cout << "\n\n\tA\t\t\tNissan\t\t\t$7.00\t\t";

    cout << "\n\n\tB\t\t\tFord Focus\t\t$7.00\t\t";

    cout << "\n\n\tC\t\t\tPeugeot 107\t\t$9.00\t\t";

    cout << "\n\n\tD\t\t\tVolvo V40\t\t$7.00\t\t";

    cout << "\n\n\tE\t\t\tVolksWagon\t\t$7.50\t\t";


    cout << "\n\n\n\tEnter choice of vehicle you wish to hire. (A, B, C, D or E):";
    cin >> choice;

    // Switch case statement
    switch (choice)
    {
    case 'A':
    case 'a':
    cout<< "\n\tYou selected Nissan\n\t" << endl;
    price = 7.00;
    break;
    case 'B':
    case 'b':
    cout<< "\n\tYou selected Ford Focus\n\t" << endl;
    price = 7.00;
    break;
    case 'C':
    case 'c':
    cout<< "\n\tYou selected Peugeot 107\n\t" << endl;
    price = 9.00;
    break;
    case 'D':
    case 'd':
    cout<< "\n\tYou selected Volvo V40\n\t" << endl;
    price = 7.00;
    break;
    case 'E':
    case 'e':
    cout<< "\n\tYou selected VolksWagon\n\t" << endl;
    price = 7.50;
    break;
    default:
    cout << "\n\tThe choice of vehicle can not be found!\n\t"<<endl;
    break;
    }

    cout << "\tEnter number of days you wish to hire it for. ";
    cin >> numdays;

    cost = price*numdays;

    cout << "\n\tThe cost of hire for this car is "<<char(156)<< cost << endl;
    cout << "\n\tDo you wish to hire this car at this rate? (Y/N): ";
    cin >> option;

    system ("cls");
    // Entry of neither "y" or "Y" will initiate loop
    }while (option != 'Y' && option != 'y');

    system("cls");
    cin.get();
    }

    // function for entry of customer details
    void customerdetails (char name[],char surname[],int addressn,
    char adres[],char PNum[])
    {
    cout << "\nEnter first name: "; //Print out
    cin >> name; // input
    cout << "\nEnter surname name: ";
    cin >> surname;
    cout << "\nEnter house number: ";
    cin >> addressn;
    cout << "\nEnter your address: ";
    cin >> adres;
    cout << "\nPlease enter Phone Number: ";
    cin >> PNum;
    cout << "\n\tYour invoice will now be displayed\n"<< endl;
    system("pause");
    }

    // Main Function
    int main ()
    {
    using namespace std;

    char name[50], surname[50], adres[50], PNum[50];
    int addressn;

    system ("color f6"); // System Colour


    carchoice (); // Call up carchoice function#


    cout << "\n\t\t\tHire and Personal Details Form\t\t\n\n";

    // Call up customer details function
    customerdetails (name,surname,addressn,adres,PNum);

    system("cls");
    cin.get();

    // Display invoice
    cout << "\n\n\t======================================================="<< endl;

    cout << "\n\t\t\tInvoice Details" << endl;

    cout << "\n\n\tCustomer Name: "<< name <<" "<< surname << endl;

    cout << "\n\n\tVehicle Type: ";
    //If,else statement to display the the car that user has selected in the invoice
    if (choice == 'a' || choice == 'A' )
    { cout << "Nissan" << endl; }

    else if ( choice == 'b' || choice == 'B' )
    { cout << "Ford Focus" << endl; }

    else if ( choice == 'c' || choice == 'C')
    { cout << "Peugeot 107" << endl; }

    else if ( choice == 'd' || choice == 'D')
    { cout << "Volvo V40" << endl; }

    else if ( choice == 'e' || choice == 'E')
    { cout << "VolksWagon" << endl; }

    cout << "\n\n\tNumber of days: "<< numdays << endl;

    cout << "\n\n\tRental Cost: "<< cost << endl;

    cout << "\n\n\tHome Address: "<< addressn <<" "<< adres << endl;

    cout << "\n\n\tPhone Number: "<< PNum << endl;

    cout << "\n\n\t======================================================="<< endl;

    cin.ignore();
    return 0;
    }
    void line()
    {
    for(int i=1; i < 41; i++)
    cout <<"__";}

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    Re: Desperately need help. have little time :(

    Firstly, when posting code please use code tags. Go advanced, select the code and click '#'.

    it just goes loco
    In what way? What happens? Have you tried to debug the code using the debugger to see where execution deviates from that expected from the program design?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,364

    Re: Desperately need help. have little time :(

    Code:
    void customerdetails (char name[],char surname[],int addressn,
    char adres[],char PNum[])
    note that you are passing addressn by value and not by ref so the changes made to addressn in the function will not be passed back to the caller.

    When you try this program are the details you enter have less characters than the size of the arrays you are declaring? If, say, the entered address has more than 49 chars then buffer overflow will occur. Why not use the class string rather than an array of char? See http://www.cplusplus.com/reference/string/string/ Using class string means that you don't need to bother about the size of the string and whether the input will overflow it.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center