Link to home
Start Free TrialLog in
Avatar of AndyCV6
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.get() ) + 10;
            
      while (cin.peek() != '\n') {
            num += static_cast< double >( cin.get() ) / place;
            place *= 10;
      }
      
      cin.ignore();
      return num;
}
SOLUTION
Avatar of lakshman_ce
lakshman_ce

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AndyCV6
AndyCV6

ASKER

I am trying to learn about cin.peek(). Is there any reason why that it would not work like I have it? Doesn't cin.peek() look at each character? In the first loop, I want it to look at each character and stop when it comes to a '.' or a newline. My end result from this program should be two numbers formated like so:
x is +2.346000000
y is      8.95e-001

I will check out that link you gave me and see if that helps.

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AndyCV6

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;
}

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial