Hi again,

My logic is not too good. I am supposed to get this function to be used twice for two different inputs calling the "strings" from within the function and returning the values to main (perhaps my mistake is attempting to call this program twice??? except that is what the instructor has asked for). I can get that to work, but I am trying to get it so it returns the values input when asked for the correct string. I am having trouble finding a loop that I can use to get it to ask for terms the first time through, then the display otions the second time.... I am VERY good at looping my machine forever, but no luck geting the alternate questions and answers to work.

suggestions?

Thanks,

Cougarclaws.

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//function to read inputs from the user test for proper input 
//and return the value to main if input is invalid, 
//the user will be reprompted for proper input

int validInput ( )
{
    
	int userIn1 = 0;
	int userIn2 = 0;
	int count = 0;
    int displayCount = 0;
    string userInput = "Please enter the number of terms to use: ";
    string userDisplay = "Please enter how often to display results: ";
           
           for (int count = 0; count == 0; count++)
               if (count == 0)
                   cout << userInput;
                 
           cout << userDisplay;
           cin >> userIn1; cout << endl;
               
           while ( userIn1 < 1)
                 {
                 cout << "Error, cannot be 0 or negative" << endl;
                 cout << userInput;
                 cin >> userIn1; cout << endl;
                 }
                                       
           return userIn1;
}

int main()
{
    
    double pi = 0;
    int numCalc = 0;
    int userIn1 = 0;
    int times = 0;
    cout << "This program calculates the value of PI to varying degrees of accuracy " << endl;
    cout << endl;
    cout << "The accuracy is based on the desired number of calculations requested by user" << endl;
    cout << "The user may also decide the sequence of displayed output during calculations" << endl;
    cout << endl << endl;
    
    numCalc = validInput ();
    times = validInput ();
    
    
    // Calculating pi/4
    for (double n = 1; n <= numCalc; n++)


    {
        pi += (double) pow(-1, n+1)/(2*n-1);
    }
    // Calculating pi
    pi *= 4;
     

   cout << endl << endl;
   system ("PAUSE");
   return 0;
   
}

Recommended Answers

All 4 Replies

My guess is that you are supposed to pass the string as an argument to the function or display the prompt outside of the function. Regardless, defining the two prompts inside the function seems wrong:

int validInput ( string prompt)
{
    cout << prompt << endl;
    // get user input using do-while loop that checks for non-positive input, then return it.
}

int main ()
{
    // code
    string calcInputPrompt = "Please enter the number of terms to use: ";
    string userDisplayPrompt = "Please enter how often to display results: ";

    numCalc = validInput (calcInputPrompt);
    times = validInput (userDisplayPrompt);
    
    //  more code
}

Look above

commented: What's this post supposed to mean? -4
commented: I think he was referring to my post here and posted the same thing, so he erased it. +22

OK, I changed it and I think I can make it work, but it won't compile.

"too many arguments to function int valid input [std::string]

Can you see what I an doing wrong?

Thanks again!

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//function to read inputs from the user test for proper input 
//and return the value to main if input is invalid, 
//the user will be reprompted for proper input

int validInput (string prompt)

{
    
	int userIn1 = 0;
	
           cin >> userIn1; cout << endl;
               
           while (userIn1 < 1)
                 {
                 cout << "Error, cannot be 0 or negative, please try again" << endl;
                 cin >> userIn1; cout << endl;
                 }
                                       
           return userIn1;
}

int main()
{
    
    double pi = 0;
    int numCalc = 0;
    int userIn1 = 0;
    int times = 0;
    string userInput = "Please enter the number of terms to use: ";
    string userDisplay = "Please enter how often to display results: ";
    
    cout << "This program calculates the value of PI to varying degrees of accuracy " << endl;
    cout << endl;
    cout << "The accuracy is based on the desired number of calculations requested by user" << endl;
    cout << "The user may also decide the sequence of displayed output during calculations" << endl;
    cout << endl << endl;
    
    cout << userInput << endl;
    
    numCalc = validInput (userInput, "Prompt");
    
    cout << userDisplay << endl;
    
    times = validInput (userDisplay, "Prompt");
    
    
    // Calculating pi/4
    for (double n = 1; n <= numCalc; n++)


    {
        pi += (double) pow(-1, n+1)/(2*n-1);
    }
    // Calculating pi
    pi *= 4;
     

   cout << endl << endl;
   system ("PAUSE");
   return 0;
   
}

I think I got it!

Thank you soooo, much again!

Cougarclaws

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.