I,m having a problem with my program.

float num,num1,score;

cin>>num;

score=num/num1;

cout<<score;

//if num and num1 is a digit, continue program; else, end program

I already tried using (isalpha and isdigit), but it seems to be working only in int and char data type, also tried using stringstream approach, but score don't give the result I need. suggestion please.

thanks in advance..

Recommended Answers

All 3 Replies

>> but it seems to be working only in int and char data type

num1 is uninitialized so it could be anything, which means score could be anything. It shouldn't work at all, whether you're using floats, ints, chars, whatever.

//if num and num1 is a digit, continue program; else, end program

This doesn't make sense. num and num1 are defined as floats. You'd better only enter digits, a minus sign, or a decimal point. If you enter something like "Hello World" as input, since you are reading from stdin DIRECTLY into a float, the stdin stream is immediately broken. If what you're trying to do is test whether something is a valid float, read it into a string, then check it for validity, then convert it. That may be the "stringstream approach" you are describing.

But regardless, num1 is uninitialized so even if the input is a valid float, it's still broken.

Do you mean that you are trying to handle something like when the user enters "flubbix" instead of a number?

Make yourself a little function to get numbers from the user:

#include <iostream>
#include <string>
#include <sstream>

template <typename T>
std::istream& get_number( std::istream& ins, T& x )
  {
  // Read a line (terminated by ENTER/RETURN key press) from the user
  std::string s;
  std::getline( ins, s );

  // Get rid of any trailing whitespace
  s.erase( s.find_last_not_of( " \f\n\r\t\v" ) + 1 );

  // Read it into the target type
  std::istringstream ss( s );
  ss >> x;

  // Check that there is nothing left over
  if (s.length() == 0 || !ss.eof())
    {
    ins.setstate( std::ios::failbit );
    }

  return ins;
  }

//
// SOMETHING TO SEE IT WORK
//
int main()
  {
  using namespace std;

  double d;

  cout << "Enter a number> ";

  if (!get_number( cin, d ))
    {
    cout << "Hey! That was not a number!\n";
    return 1;
    }

  cout << "Good job! Your number was: " << d << ".\n";
  return 0;
  }

Remember the following cardinal rule:

The user will always press ENTER/RETURN at the end of every input!

Hope this helps.

sorry, a typo error,,

this is what I mean;

START:

float num,num1,score;

cout<<"enter 1st number"<<endl;

cin>>num;

cout<<"enter 2nd number"<<endl;

cin>>num1;

score=num/num1;

cout<<score;

// if num or num1 is not digit, print "invalid input" and goto START; else continue.
//this is only a part of code inside a loop;

since this program must be user friendly, a character input will result a program to return to START. The suggestion from Duoas seems to be working, although I don't understand the whole concept, my knowledge in C++ is limited in using function and still learnin. Thanks to all, who gave their suggestions and to those who will.

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.