Jump to content

Check out our Community Blogs


Member Since 12 Sep 2010
Offline Last Active Jan 05 2011 11:09 PM

Topics I've Started

Confused as to how to make a .dat file? (To test ifstream/ofstream)

23 November 2010 - 10:29 PM

I am not in school which finally became a problem since my text book assumes I am and tells me to get the .dat files from my instructor. Thats so I am able to test the <fstream> and its uses. Unfortunitly as I mentioned, I am teaching my self C++ and not in school. This being said, I have no idea how to make these .dat files my book is refering too. I tried to open a notepad and save it with that extension (after adding a few integers). It became unreadable and I had high hopes. But no such luck. Perhaps my code was wrong? If its not, anyone know how to make a file I can use to test these function calls and streams?

#include <iostream>
#include <fstream>
using namespace std;

int main()
    ifstream in_stream;
    ofstream out_stream;


    int a, b, c, d;

    in_stream >> a >> b >> c >> d;
    cout << a << b << c << d;

test.dat is just a file with 1, 2, 3, 4 inside. This program outputs a bunch of random numbers that I cant explain. Maybe you can? ;)

Thanks in advance!

Super annoying issue with the 'char' type

02 November 2010 - 01:16 AM

void calculation(int& calc1, int& calc2, char& merid)
    using namespace std;
    //char* AM = "AM";
    //char* PM = "PM";
    //char AM[] = "text";
    //AM[0] = 'AM';
    //char PM[] = "text";
    //PM[0] = 'PM';
    //char* AM = new char('AM');
    //char* PM = new char('PM');
    char* AM = "AM";
    char* PM = "PM";

    if (calc1 < 12)

        merid = AM[3];
    calc1 = (calc1 - 12);
    merid = PM[3];

I have tried so many different options, googled this issue a dozen times and sat here staring at it for an hour and a half... Why can I not make transfer the value of PM or AM to merid?
I understand now that when you set a 'char' a value, it becomes a constant, but what I dont understand is why I cant make another variable take that value.
How would I go about fixing this?

Thanks in advance~

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

01 November 2010 - 10:23 PM

//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;


        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;
//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];

    [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.

Possible logic error?

30 October 2010 - 02:58 AM

This is a program I made to calculate the price of each stock you own. (for practice while I learn)

//program calculates the price of one stock
#include <iostream>
using namespace std;

//function call
double price_per(int whole, int numer, int denom, int total_shares);

//main program
int main()
    char loop;

        //gathers input
        int number_of_shares, whole_value, numerator, denominator;
        double price_per_share;
        cout << "Enter the number of shares you own: ";
        cin >> number_of_shares;
        cout << "Enter the value of your shares, first the whole value: ";
        cin >> whole_value;
        cout << "Now enter the numerator: ";
        cin >> numerator;
        cout << "Now the denominator: ";
        cin >> denominator;

        price_per_share = price_per(whole_value, numerator, denominator, number_of_shares);
        //sets precision
        //displays output
        cout << "The value of one share is: ";
        cout << price_per_share << endl;
        cout << "Enter Y to continue or N to exit: ";
        cin >> loop;

    }while (loop == 'Y' || loop == 'y');
    cout << "~~~~~~~~~~~~~~~~~~~Goodbye~~~~~~~~~~~~~~~~~~~" << endl;

//function definition
double price_per(int whole, int numer, int denom, int total_shares)

    double whole_denom_numer, last;

    whole_denom_numer = (((whole*denom)+numer)/denom);
    last = whole_denom_numer/total_shares;
    return last;

I have been at it for about 40 minutes and I made a lot of progress but now I am stuck with any answer giving me ".00" instead of the actual number after the decimal point.
Not sure why this is happening but I can only assume my math is wrong since the program seems to run fine... Any help would be nice.

Thanks in advance~

In case you wish to read the actual exercise: The price of stocks is sometimes given to the nearest eighth of a dollar; for example, 29 7/8 or 89 1/2. Write a program that computes the value of the users holding of one stock. The program asks for the number of shares of stock owned, the whole dollar protion of the price and the fraction portion. The fraction portion is to be input as two int values, one for the numerator and one for the denominator. The program then outputs the value of the users holdings. Your program should allow the user to repeat this calculation as often as the user wishes. Your program will include a function definition that has three int arguments consisting of the whole dollar portion of the price and the two integers that make up the fraction part. The function returns the price of one share of stock as a single number of type double.

I am aware that my function definition has four arguments. I couldnt for the life of me figure out how to call the total number of shares from the main program to the function definition in order to use it. So I felt I had no other choice but to drag it down using the function call.

INT vs Double?

26 October 2010 - 04:33 PM

I am most likely wrong but I just did a test using both INT and Double. Double was capable of holding both whole and not whole numbers whereas INT was only capable of holding whole numbers. To my understanding Double also holds much larger numbers as well as smaller numbers. Why would I choose to use INT where I can use double? Even if I know the number will be whole like asking for x number of months in a year. Why not just use double?

Thanks for the speedy reply!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download