Hi all,

I have tried this several ways. I need to get the user function to return userIn1 to main. I don't need to read anything from the call to the function, but if I leave out paraneters, it really does not work. My original issue was that the string value and print outputs are not showing up. I believe this is because of my inability to fix the function. I will have to use the function for this assignment twice, so if there is more I need to do to get that to work (once built for displayCount), please guide me. Thanks for all the help in advance!

I have not even started the PI accuracy side of this yet, so since I know this is a common HW assignment, any help with understanding how the formula would get written is appreciated as well.

#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 repormpted for proper input

int validInput (int)
{
    
               
           string userInput = "Please enter the number of terms to use: ";
           int userIn1 = 0;
           int displayCount = 0;
           
           
           cout << userInput;
           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()
{
    
    int numCalc = 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 (userIn1);
    
    
    
     

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

where are you defining the userIn1 in your main function?

If you want to validate that the user input is a number then
you can do it like so :

int num = 0;
  cout<<"Enter a number : ";
  cin >> num;
  whlie(!cin ) // check if the user inputs a valid data, i.e a number in this case
{
    cin.clear(); //clear the stream
    while( cin.get() != '\n')  //read all the fail input
     ;
     cout<<"\nPlease enter a number : ";
     cin >> num;
}

You rock, in delcaring the variable "userIn1" in my main, The program now will run ( I did this before) , but I still get no output for the user input from the function. My "userInput string" does not show up.
Any ideas?
And thank you very much,
Cougarclaws

This is what it looks like now.

#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 repormpted for proper input

int validInput (int userIn1)
{

    int displayCount = 0;
    string userInput = "Please enter the number of terms to use: ";
           
           cout << userInput;
           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()
{
    
    int numCalc = 0;
    int userIn1 = 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 (userIn1);
    
    
    
     

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

I assume you want something like this :

int getInput ( int input) 
{
    cin >> input;
    cout << "your input : "<<input<<endl;
   return input;
}

In your function, you have validInput(int). It should be
int valudInput() // no parameters, since you want ask the user
to input a value inside the function. But the return type
should be an int if you want it to return the user's input.

Also this condition

while ( userIn1 < 1 ) //means the if userInput is 1 or below reject it.

If you want to print the users input then in your main you can
do this :

int main() 
{
   ...
   ....
     numCalc = validInput (userIn1);
   cout<<numCalc<<endl;
}

Thank you again! The program will not compile if I use () in the function. If I do not declare userIn1 in main, then I get "too many arguments". I am very confused. My instruction for this are :
Write a user defined function to read input from the user. The requirements for the function are:
The prompt for user input will be stored in a STRING type variable and passed as an input argument to the function.

The function will prompt the user for input and then use a loop to verify that the user input is positive and non 0. If not, the fuction returns an error and request for the input again.

Once valid input is entered, the value gets passed back to main.

So, am I that far off??

Thanks,

Cougarclaws

I guess I don't understand why my string does not print to the screen and the user get challenged for the input.

Cougrclaws

I have changed a little to your code ( which I mention in the code) :

#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 repormpted for proper input

int validInput ( ) //CHANGED
{

	int userIn1 = 0;//ADDED
    int displayCount = 0;
    string userInput = "Please enter the number of terms to use: ";
           
           cout << userInput;
           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()
{
    
    int numCalc = 0;
    int userIn1 = 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 ();
    
    
    
     

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

But I am not sure which part you are stuck on. What compiler
are you using? Are you saying that this

string userInput = "Please enter the number of terms to use: ";                         cout << userInput;

does not print the string?

I think you fixed it. I was looking at the starting parameters and not the return to main. Yours compiles just as it should.

You are awesome. I think I now have a formula for Pi as well.

Thank you for all the time you spent with me. I hope one day to do the same for others. My reputation here took a hit on a very frustrating day, and I was just trying to point out that I wasn't getting any suggestions to fix my issues, just that they were bad.

I am a Network Engineer trying to learn this, so I am way out of my comfort zone and feel a bit intimidated.

You have been great!!!
Thanks again!

Cougarclaws.

Good Luck :)

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.