There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
C++ help


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
payback's Avatar
Junior Member with 15 posts.
 
Join Date: Jul 2003
31-Mar-2005, 01:21 AM #1
C++ help
I am working on a programming assignment using C++ and am lost as to how to make a group of answers print to my file called "mathops.out". I have a list with + 2 14, + 528 826, * 2 64, / 6 2 and am trying to have my program compute these problems and have them output to my file. Any ideas?
The program runs as is but it will only output one answer when I use ./a.out and then enter in + 2 14 I want more.

Thanks

#include <iostream>
#include <fstream>

using namespace std;

void ShowMathTable(ofstream & fOut);
int Add(int a, int b);
int Subtract(int a, int b);
int Multiply(int a, int b);
int Divide(int a, int b);
int Mod(int a, int b);

int main()
{
ofstream fOut;

fOut.open("mathops.out",ios:ut);

if(!fOut)
{
cout << "Unable to open..." << endl;
}
ShowMathTable(fOut);

fOut.close();
return 0;
}

void ShowMathTable(ofstream & fOut)
{
char op; // operator
int a, b; // variables
int result;

cout << "Enter an expression "<< flush;
cout << "in the form: op number number"<< endl;
cin >> op >> a >> b; // inputs values here

if(op == '+')
{
result = Add(a, b);
cout << a << "+" << b << "=" << result << endl;
fOut << a << "+" << b << "=" << result << endl;
}

if(op == '-')
{
result = Subtract(a, b);
cout << a << "-" << b << "=" << result << endl;
fOut << a << "-" << b << "=" << result << endl;
}

if(op == '*')
{
result = Multiply(a, b);
cout << a << "*" << b << "=" << result << endl;
fOut << a << "*" << b << "=" << result << endl;
}

if(op == '/')
{
result = Divide(a, b);
cout << a << "/" << b << "=" << result << endl;
fOut << a << "/" << b << "=" << result << endl;
}

if(op == '%')
{
result = Mod(a, b);
cout << a << "%" << b << "=" << result << endl;
fOut << a << "%" << b << "=" << result << endl;
}
}


int Add(int a, int b) // Add - add two integers, return result
{
int result;
result = a + b;
return result;
}

int Subtract(int a, int b) // Subtract - subtract two integers, return result
{
int result;
result = a - b;
return result;
}

int Multiply(int a, int b) // Multiply - multiply two integers, return result
{
int result;
result = a * b;
return result;
}

int Divide(int a, int b) // Divide - divide two integers, return result
{
int result;
result = a / b;
return result;
}

int Mod(int a, int b) // Mod Operator - mod two integers, return result
{
int result;
result = a % b;
return result;
}
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
31-Mar-2005, 01:29 PM #2
You need to be clearer. Do you want a loop that keeps asking to enter another set unless you tell it no?

Until I know, here are a few things.


Set up your main() like this:

Code:
int main() { 
    const string file("mathops.txt");
    ofstream out( file.c_str() );
    if (!out) {
        cout << "\n" << "Error writing to " << file << endl;
        return 1;
    }
    ShowMathTable(out); 
}
The return line at the end of main() is redundant.

The stream doesn't have to be manually closed. It will be destructed when main() ends. Of course if you need to access the file again with a different stream, then you'd need to close it, but there are better ways for that situation.

You do not need to use the .open() member function as you can see above.

Also, if you put your functions above main(), you won't need the 6 lines so main() can find your functions.

Also, your functions are not really needed and your result variable is not really needed. You can just cout << (a -b) and be done with it.

If you do want to use the functions, you can pass by reference (since you are not modifying) and just return the answer.

Code:
int Multiply(const int& a, const int& b) {
    return a * b;
}
Also, since you are using integers, when you do 4 divided by 5 for example, it's going to show up as 0; You should use floats. Of course you need to handle the % operator then. So if x and y are floats, you can do cout << ( (int)x % (int)y ) << endl;

If you do indeed need a loop, we can show you a general example and if you show effort, then we can show you a more specific example.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
payback's Avatar
Junior Member with 15 posts.
 
Join Date: Jul 2003
31-Mar-2005, 10:55 PM #3
Thank you very much for your help it made my program more clear and easier to work with.

Thanks
PayBack
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
01-Apr-2005, 06:45 AM #4
Here's are a couple examples of how you can use a loop to keep asking something unless the correct condition is met.

Code:
#include <iostream>
#include <string>

using namespace std;

int main() {
    for (bool done = false; !done; ) {
        cout << "\n" << "What's 5 + 5? ";
        string a;
        getline(cin,a);
        if (a == "10") {
            done = true;
        } else {
            cout << "\n" << "wrong, try again" << endl;
        }
    }
}
Code:
#include <iostream>
#include <string>

using namespace std;

int main() {
    for ( ; ; ) {
        cout << "\n" << "What's 5 + 5? ";
        string a;
        getline(cin,a);
        if (a == "10") {
            break;
        } else {
            cout << "\n" << "wrong, try again" << endl;
        }
    }
}
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:39 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.