AndyCV6
asked on
Need help with a while loop using cin.peek()
I need help finding some logic errors. My program is getting stuck in the two while loops. This program takes two numbers and then formats the output to scientific notation. Here is the first part of my code. Any help would be greatly appreciated!
int main()
{
double x, y;
x = readNum();
y = readNum();
//printFormatted( x, y );
return 0;
}
double readNum()
{
double num = 0;
double place = 10;
char numb[20];
cout << "Enter a number: ";
cin.get(numb, 20);
while (cin.peek() != '.' || cin.peek() != '\n')
num *= static_cast<double>(cin.ge t() ) + 10;
while (cin.peek() != '\n') {
num += static_cast< double >( cin.get() ) / place;
place *= 10;
}
cin.ignore();
return num;
}
int main()
{
double x, y;
x = readNum();
y = readNum();
//printFormatted( x, y );
return 0;
}
double readNum()
{
double num = 0;
double place = 10;
char numb[20];
cout << "Enter a number: ";
cin.get(numb, 20);
while (cin.peek() != '.' || cin.peek() != '\n')
num *= static_cast<double>(cin.ge
while (cin.peek() != '\n') {
num += static_cast< double >( cin.get() ) / place;
place *= 10;
}
cin.ignore();
return num;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, i read the info from the link, and what efn posted. I made some changes to my code. It will now break out of the loops but, my output is still not right. I am not sure what to do about initializing num. Let me post all of my code for this program.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::ios;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::resetiosflags;
using std::setfill;
using std::setiosflags;
double readNum();
void printFormatted( double, double );
int main()
{
double x, y;
x = readNum();
y = readNum();
printFormatted( x, y );
return 0;
}
double readNum()
{
double num = 0;
double place = 10;
cout << "Enter a number: ";
while (cin.peek() != '.' && cin.peek() != '\n')
num *= static_cast< double >( cin.get() ) + 10;
//cin.ignore();
while (cin.peek() != '.') {
num += static_cast< double >( cin.get() ) / place;
place *= 10;
}
cin.ignore();
return num;
}
void printFormatted( double x, double y )
{
char buffer[] = "The value of x is: ";
for( int i = 0; buffer[ i ] != '\0'; i++ )
cout.put( buffer[ i ] );
cout << setprecision( 3 ) << setfill( '0' )
<< setiosflags( ios::fixed | ios::showpos )
<< setiosflags( ios::left ) << setw( 12 ) << x << endl;
cout.write( "The value of y is: ", 21 );
cout << resetiosflags( ios::showpos )
<< setprecision( 3 )
<< setiosflags( ios::scientific | ios::right )
<< y << endl;
}
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::ios;
#include <iomanip>
using std::setw;
using std::setprecision;
using std::resetiosflags;
using std::setfill;
using std::setiosflags;
double readNum();
void printFormatted( double, double );
int main()
{
double x, y;
x = readNum();
y = readNum();
printFormatted( x, y );
return 0;
}
double readNum()
{
double num = 0;
double place = 10;
cout << "Enter a number: ";
while (cin.peek() != '.' && cin.peek() != '\n')
num *= static_cast< double >( cin.get() ) + 10;
//cin.ignore();
while (cin.peek() != '.') {
num += static_cast< double >( cin.get() ) / place;
place *= 10;
}
cin.ignore();
return num;
}
void printFormatted( double x, double y )
{
char buffer[] = "The value of x is: ";
for( int i = 0; buffer[ i ] != '\0'; i++ )
cout.put( buffer[ i ] );
cout << setprecision( 3 ) << setfill( '0' )
<< setiosflags( ios::fixed | ios::showpos )
<< setiosflags( ios::left ) << setw( 12 ) << x << endl;
cout.write( "The value of y is: ", 21 );
cout << resetiosflags( ios::showpos )
<< setprecision( 3 )
<< setiosflags( ios::scientific | ios::right )
<< y << endl;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
x is +2.346000000
y is 8.95e-001
I will check out that link you gave me and see if that helps.
Thanks