Add statement after if statement

So, my program is all written out but I don't know how to use a switch statement to display a message on the output. For example,

if (miles_ran == 40)
{I would like to output to say "Good Run!"}
else if (miles ran >= 40)
{"I would want the output to say "Try Harder!"

and say my output section already looks like

cout << setw(15) "How you did today" << endl;

And output already is

Miles Ran: How you did today:

and I want it to be either

Miles Ran: 40 How you did: Great!


Miles Ran: 40
You did Great!

I actually only need to display a message for when the input is default, but I think the solution should be the same.


Also, like previously mentioned I already have my entire program written out. Only problem is I need it written to an output file. I've desperately tried searching the internet for a solution but I find it rather difficult to understand after I've already entered everything. I guess I can make up an example:

#include <fstream>
#include <iostream>

using namespace std;

int job_ud
double dist_trvl

cout << "Enter Job: ":
cin >> job_ud

cout << "Enter distance traveled: ";
cin >> dis_trvl;

if (dis_trvl = 20)
{ comp = 50}
else if(dis_trvl <= 20)
{ comp = 20;|}

cout << "Job title: " << job_ud << "distance traveled" << dis_trvl << endl;

this is the only thing that i understand
Your if/else if conditions are not properly written. As of now, you have two possible output messages for when miles_ran is 40. Try using else if(miles_ran < 40) instead.

For a switch you could simply do:

    case 40: cout << "Good run";
    default: cout << "Try harder";

That is of course assuming miles_ran can't be over 40, because then we'd have another problem.
Ok, what about the second part. How would I send output to file?
make an output file variable like this :

ofstream variablename("filename.txt", ofstream::out | ofstream::app);

if (!
   //write data to file 
Also, to write data to a file, you would do it in the same prices as a cout statement. Just replace "cout" with the name of your ofstream variable
The file is coming up blank. I put that in the output section right? I changed all my outputs to outfile but now it's all blank.


Oh and another question in regards to my first question one.

case 40: cout << "Good run";
default: cout << "Try harder";

is fine but I need "Good Run" to appear at the output, not immediately they enter 40. For example, I want it to look like

Miles Ran: Distance Traveled:
40 40
"Good Run!"

In the example you provided "good run!" would appear immediately after inputting 40. I don't want it to do that.
Last edited on
Topic archived. No new replies allowed.