C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 30th, 2006, 10:09 AM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
Some help?

Hello all, I'm a new programmer and I just learned about functions.

I made this program to kinda help my little sis with her math and to give me some practice making a simple program using functions. What I would like to know is, how can I get the program not to end after one problem, and to just return to the beginning to do the whole process over again? would it involve loops?

Your help would be much appreciated, thanks in advance.


Code:
#include <iostream>
using namespace std;

int sumSequence(int, int, int);
 

int main(int nNumberofArgs, char* pszArgs[])
{
    int a;
    int b;
    int c;
    int d;
    cout << "Enter two totals, the product (multiplication) of those two will be calculated\n" 
         << "you then must enter the correct answer\n" 
         << endl;
    system("PAUSE");
    cout <<"\n"<<endl;
         
         // this takes the first number entered and stores it in variable a.
    cout << "Enter a number:"<<endl; 
    cin  >> a;
         
         /* this takes the second number and stores it in variable b, then
          sends it off to function sumSequence.*/
    cout << "Enter another number: "<<endl;
    cin  >> b;
    c = sumSequence(a,b,c);
    
     
        // this requests the user to answer his own problem.
    cout << "please enter your answer: "<<endl;
    cin  >> d;
    
        // if the user's answer is incorrect the user is then corrected.
    if (d != c)
    { 
          cout << "Sorry but that was incorrect, the answer is actually..." << c << endl;
    }
    
       // if the user's answer was correct then he/she is praised.
    else {
         cout << "Good job, "<<a<<" x "<<b<< " = "<<c<<" You are correct." <<endl;
         }
    system("PAUSE");
    return 0;
         }


        int sumSequence(int a, int b, int c)
        {
        c = a * b;
        cout << "Thank you " << endl;
        
        return c;
        }

Reply With Quote
  #2  
Old December 30th, 2006, 12:19 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Use a do while loop.

Reply With Quote
  #3  
Old December 30th, 2006, 01:29 PM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
thanks, but

I tried including a do while loop, but can't figure out where to place the do and the while.

it keeps giving me a long list of errors. can you possibly show me where the do and while would go in order to make it work, I tried the do, right after int main, and put the while at the end of main, but then it would give me errors regarding my int function at the end. I've went over loops already, but I'm lost on this one.

Reply With Quote
  #4  
Old December 30th, 2006, 01:40 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
You could do it like this:

Code:
#include <iostream>

using namespace std;

int sumSequence(int, int, int);


int main(int nNumberofArgs, char* pszArgs[])
{
    int a;
    int b;
    int c;
    int d;
    char tryAgain;


    cout << "Enter two totals, the product (multiplication) of those two will be calculated\n"
         << "you then must enter the correct answer\n"
         << endl;
    system("PAUSE");
    cout <<"\n"<<endl;

    do
    {

         // this takes the first number entered and stores it in variable a.
    cout << "Enter a number:"<<endl;
    cin  >> a;

         /* this takes the second number and stores it in variable b, then
          sends it off to function sumSequence.*/
    cout << "Enter another number: "<<endl;
    cin  >> b;
    c = sumSequence(a,b,c);


        // this requests the user to answer his own problem.
    cout << "please enter your answer: "<<endl;
    cin  >> d;

        // if the user's answer is incorrect the user is then corrected.
    if (d != c)
    {
          cout << "Sorry but that was incorrect, the answer is actually..." << c << endl;
    }




       // if the user's answer was correct then he/she is praised.
    else {
         cout << "Good job, "<<a<<" x "<<b<< " = "<<c<<" You are correct." <<endl;
         }

    system("PAUSE");

    cout << "Do you want another go? <y/n> : ";
    cin >> tryAgain;

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

    return 0;
}


int sumSequence(int a, int b, int c)
{
    c = a * b;
    cout << "Thank you " << endl;

    return c;
}

Reply With Quote
  #5  
Old December 30th, 2006, 01:44 PM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
ah!

ok thanks that worked.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Some help?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap