Jump to content

New to "call by reference" and "void functions", few errors in program

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Steamsteam

Steamsteam

    Newbie

  • Members
  • PipPip
  • 27 posts

//program calculates military time to standard time

#include <iostream>

//function declarations

double input(int& hour, int& minute);

//function to gather the military time

double calculation(int hour_calc, int minute_calc, char& merid);

//function to calculate the military time

void output(int hour_out, int minute_out);

//function to output the results

void initialize();

//function to initialize the screen


//main program

int main()

{

    char loop;

    using namespace std;


    do

    {


        int hours, minutes;

        char meridi;


        [COLOR="red"]double input(hours, minutes);

        double calculation(hours, minutes, meridi);

        double output(hours, minutes);[/COLOR]





    }while (loop == 'Y' || loop == 'y');

    cout << "Good bye";

    void initialize();

    return 0;

}

//function initializes screen

void initialize()

{

    using namespace std;

    cout << endl;

    return;

}

//function gathers nessasary input from user

double input(int& hour, int& minute)

{


    using namespace std;

    cout << "Please enter the military time, the two first digits: ";

    cin >> hour;

    cout << "Please enter the last two digits of the military time: ";

    cin >> minute;


}

//function calculates the data gathered from input and monitors weather its AM or PM with "char merid"

double calculation(int hour_calc, int minute_calc, char& merid)

{


    using namespace std;

    [COLOR="red"]char daytime = (char)merid;[/COLOR]


    if(hour_calc > 11)

    {


    hour_calc = hour_calc - 12;

    [COLOR="red"]daytime = "PM"[/COLOR];

    }

    else

    {


    [COLOR="red"]daytime = "AM";[/COLOR]

    }

}

//function outputs results of the calculation

void output(int hour_out, int minute_out)

{

    using namespace std;


    cout << "Your time in standard is: " << hour_out << ":" << minute_out << endl;


}

I had quiet a few more errors but I have limited it to five:

||In function 'int main()':|

[COLOR="red"]|25|error: initializer expression list treated as compound expression|

|26|error: initializer expression list treated as compound expression|

|27|error: initializer expression list treated as compound expression[/COLOR]|

||In function 'double calculation(int, int, char&)':|

[COLOR="red"]|66|error: invalid conversion from 'const char*' to 'char'|

|71|error: invalid conversion from 'const char*' to 'char'|[/COLOR]

||=== Build finished: 5 errors, 0 warnings ===|




I never made merid a constant, and I tried to use (char) to change it from a constant to a variable with no luck. And I am not sure what "initializer expression list treated as a compound expression" means.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The first block you are calling "double" before each function, thus declaring a prototype within the main function which is disallowed.

In your second error's code, you set up daytime and merid to be type char, but "AM" or "PM" is a char array not a single char so you will need to define them as so, also not to cast (char) to merid. I see you are using C++ so you may wish to use std::string rather than char[] or char*
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Steamsteam

Steamsteam

    Newbie

  • Members
  • PipPip
  • 27 posts

//program calculates military time to standard time

#include <iostream>

//function declarations

double input(int& hour, int& minute);

//function to gather the military time

double calculation(int hour_calc, int minute_calc, char& merid);

//function to calculate the military time

void output(int hour_out, int minute_out);

//function to output the results

void initialize();

//function to initialize the screen


//main program

int main()

{

    char loop;

    using namespace std;


    do

    {


        int hours, minutes;

        char meridi;


        [COLOR="red"]input(hours, minutes);

        calculation(hours, minutes, meridi);

        output(hours, minutes)[/COLOR];





    }while (loop == 'Y' || loop == 'y');

    cout << "Good bye";

    void initialize();

    return 0;

}

//function initializes screen

void initialize()

{

    using namespace std;

    cout << endl;

    return;

}

//function gathers nessasary input from user

double input(int& hour, int& minute)

{


    using namespace std;

    cout << "Please enter the military time, the two first digits: ";

    cin >> hour;

    cout << "Please enter the last two digits of the military time: ";

    cin >> minute;


}

//function calculates the data gathered from input and monitors weather its AM or PM with "char merid"

double calculation(int hour_calc, int minute_calc, char& merid)

{


    using namespace std;

    [COLOR="red"]char PM, AM;[/COLOR]


    if(hour_calc > 11)

    {


    hour_calc = hour_calc - 12;

    [COLOR="red"]merid = PM;[/COLOR]

    }

    else

    {


    [COLOR="red"]merid = AM;[/COLOR]

    }

}

//function outputs results of the calculation

void output(int hour_out, int minute_out)

{

    using namespace std;


    cout << "Your time in standard is: " << hour_out << ":" << minute_out << endl;


}


Thanks! That works! Unfortunitly I have a logic error, but I think I can manage that :)

+rep




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users