0

Hello,

I'm having a bit of trouble figuring out how to get C++ to output something to a text file. Any help would be very appreciated. Here is the code:

//THIS PROGRAM IS INTENDED TO GENERATE THE SQUARE ROOT OF AN INPUTTED NUMBER USING A WHILE LOOP AND A USER
//SUBMITTED GUESS FOR THE SQUARE ROOT. THE DIFFERENCE BETWEEN THE USERS ESTIMATE AND THE ACTUAL SQUARE ROOT WILL
//BE COMPARED TO EACH OTHER UNTIL THEY ARE WITHIN AN ACCEPTABLE RANGE OF EACH OTHER.  WHEN THE TWO NUMBERS ARE WITHIN
//THE ACCEPTABLE RANGE, THIS NUMBER WILL BE RETURNED TO THE USER AS THE SQUARE ROOT.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

//VOID FUNCTIONS
void getValues(float&, float&, bool&); //RECEIVES INPUT FROM THE USER
void newGuess(float, float, float&); //CALCULATES NEXT GUESS
void swap(float&, float&); //NEXT GUESS AND LAST GUESS ARE SWAPPED IF NECESSARY


//MAIN PROGRAM
int main()
{
//DECLARED VARIABLES AND FUNCTIONS
float ng=0.0; //NEXT GUESS
float lg=0.0; //LAST GUESS
float n=0.0; //SQUARE ROOT OF A NUMBER
float difference=0.0;
bool dataok = false;
bool done = false;
int count=0;
 
getValues(n,lg,dataok);
while((done==false))
{
newGuess(n,lg,ng);
difference = (lg - ng);
if ((difference > 0.005) || (difference < -0.005))
{
cout<< "The last guess was: " <<lg <<endl;
cout<< "" <<endl;
cout<< "The next guess for the square root is: " <<ng;
swap(lg , ng);
cout<<endl;
done=false;
count++;
}
else
{
cout<< "The square root of the number is: " <<ng <<endl;
cout<< "and it took " <<count <<" attempts" <<endl;
done=true;
}
}
return 0;
}
//DESCRIPTION:GET VALUES FOR VOID FUNCTION
//PARAMETERS: MODIFY THE VARIABLES N, NUMBER, AND LG, LAST GUESS
//IN: N, NUMBER, AND LG, LAST GUESS, BY REFERENCE
//OUT: 
//INOUT:
//RETURNS: NONE

void getValues(float& a, float& b, bool& dataok) //TAKES INPUT FROM USER AND ASSIGNS THEM TO VARIABLES
{
cout<< "This is a square root calculator." <<endl;
cout<< "" <<endl;
cout<< "Input the positive number for which you want to know the square root: ";
cin>> a; //INPUT NUMBER TO FIND SQUARE ROOT
cout<< endl <<"Input the positive number that is your guess of the square root: ";
cin>> b; //INPUT NUMBER TO USE AS LAST GUESS
while(!((a>0) && (b>0) && (b<= (a/2))))
{
dataok=false;
cout<< "Sorry, one or more of your values is not acceptable.  Please try again."<<endl<<endl<<endl;
cout<< "" <<endl;
cout<< "This is a square root calculator." <<endl;
cout<< "" <<endl;
cout<< "Input the positive number for which you want to know the square root: ";
cin>> a; //INPUT VALUE FROM USER FOR NUMBER TO FIND SQUARE ROOT OF
cout<< "" <<endl;
cout<< endl <<"Input the positive number that is your guess of the square root: ";
cin>> b;
}
dataok=true;
}
//DESCRIPTION: SWAPS THE TWO VARIABLES (NEXT GUESS AND LAST GUESS)
//PARAMETERS: FLOAT A AND FLOAT B
//IN:
//OUT:
//INOUT:
//RETURNS:
void swap (float& a, float& B) //SWAPS NEXT GUESS TO LAST GUESS
{
float temp;
temp = a;
a = b;
b = temp;
}
void newGuess(float number, float lastGuess, float& nextGuess) //DETERMINES THE VALUE OF NEXT GUESS
{
nextGuess =(0.5f*(lastGuess + number/lastGuess));
std::cin.ignore();
std::cin.get();
ofstream lab4("lab4.txt");
lab4 <<"The submitted value was " << temp << "and its square root was " << nextGuess << "." <<endl; //line 103
lab4.close();
return 0; //line 105

}

the error messages i get are:

(103) : error C2065: 'temp' : undeclared identifier

and

(105) : error C2562: 'newGuess' : 'void' function returning a value

thank you!

Edited by WaltP: Fixed CODE Tags -- use the PREVIEW Button

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

Look at those two lines and see if you can't figure out what is wrong with them. Read the error messages carefully because they tell you exactly what is wrong.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.