3

I wrote a function to squire number and try to cover all the input possibilities.

Overall it works fine with numeric input, but it starts a infinite loop of printing statements on screen when I enter alphabetical input.As all we know that inside computer single character like "A or a or b or B" so on is represented by integers and as i learned from my teacher that we can store single characters into a variable with integer data type. i am not talking about strings which means collection of characters . this program create problem with single character !

#include <iostream>
#include <string>
using namespace std;
void squire();

int main() {
  squire();
}

void squire() {
  double num = 1.0, pow = 1.0, Squire_Number = 1.0;
  char ans;

  reStart:
    cout << "please Enter the Number: \n";
    cin >> num;
    cout << "please enter the nmber you want to power the number with: \n";
    cin >> pow;
    if (num > 0 && pow>0) {
      for (int i = 1; i <= pow; i++) {
        Squire_Number *= num;
        }
        cout << pow << " power of " << num << " is equil to : " << Squire_Number;
        goto option;
      }
    else
      {
        cout << "Please enter Positve Integers. \n" ;
        option:
        cout<< "\nPease type 'Y' to Enter the values again OR type 'c' to Exit ! \n";
        cin >> ans;
        if (ans == 'y' || ans == 'Y') {
          goto reStart;

        } else if (ans == 'c' || ans == 'C') {
          cout << "thank you for using our function. \n";
        }
      }

  return;
  }
LIAQAT ALI
  • 39
  • 2
  • Possible duplicate of [cin for an int inputing a char causes Loop that is supposed to check input to go wild](https://stackoverflow.com/questions/18400620/cin-for-an-int-inputing-a-char-causes-loop-that-is-supposed-to-check-input-to-go) – scohe001 Sep 24 '19 at 20:10
  • This is obviously C++. Which part of your question is C-specific? If none, why did you tag C? – Yunnosch Sep 24 '19 at 20:12
  • i declare num and pow as a double data type when i enter alphabetic value for num or pow or for both its start infinite loop ans with char data type i just used to get answer from the user when above all statements execute – LIAQAT ALI Sep 24 '19 at 20:17
  • 1
    side note: `for (int i = 1; i <= pow; i++)` is the same as `for (int i = 0; i < pow; i++)` except more people will look at it funny and wonder if you have an off by one bug. – user4581301 Sep 24 '19 at 20:24
  • Yap.. i know about for loop but that is not the problem here, dose its matter if I initialize i with 0 or 1 the problem is something else here.. – LIAQAT ALI Sep 24 '19 at 20:32
  • i never give up on this but just want to know expert opinions .for which so for waiting.. – LIAQAT ALI Sep 24 '19 at 20:35
  • `cin >> num;` can't handle alphanumeric. Just numeric. If you feed letters into it, `cin` will enter an error state where it cannot be read and returns immediately. Since the code is not checking the stream state and trying to recover from errors an infinite loop is quite likely. – user4581301 Sep 24 '19 at 20:41
  • Watch out for `goto`. It's really hard to get right and even harder to justify the use of it to your peers. Often when folks see bugs with `goto` statements involved the response is "Don't use `goto`." and no actual assistance with fixing the bug. Because `goto` usually turns out to be the bug. It isn't in this case, but I haven't explored the code deeply enough to tell you whether or not it's a different bug. – user4581301 Sep 24 '19 at 20:42
  • You could replace the `goto` and label with a `do-while` or `while` loop. – Thomas Matthews Sep 24 '19 at 20:53
  • See also `std::toupper` and `std::tolower`. This will simplify your `if` statements, such as: `if (toupper(ans) == 'Y')` – Thomas Matthews Sep 24 '19 at 20:55
  • How does your program handle floating point values for `pow`? For example, enter 4 as the first number and 0.5 as the power. The result should be 2. – Thomas Matthews Sep 24 '19 at 20:57
  • well thank you all for your valuable comments and suggestions, But all we know that inside computer single character like "A or a or b or B" so on is represented by integers as i learned from my teacher that we can store single characters into a variable with int data type i am not talking about strings which means collection of characters . this program create problem with single character ! – LIAQAT ALI Sep 25 '19 at 10:59

1 Answers1

1

Better try to read the input in an std::string, then parse the string to check if you only have numeric characters and then use std::atoi to convert the string in integer. One last recomendation, avoid to use goto instructions, this practice make a code difficult to read.

#include <iostream>
#include <string>
#include <cstdlib>

bool OnlyNumeric(const std::string& numStr)
{
    size_t  len= numStr.length();
    int i;
    for (i=0;i<len && numStr[i]  <='9' && numStr[i]  >='0';i++) ;

    return i == len;
}


int main()
{
    std::string inputStr;
    int num;

    do{
        std::cout  << "Input number:\n";
        std::cin >> inputStr;
    }   
    while (!(OnlyNumeric(inputStr)  && (num=std::atoi(inputStr.c_str())) ));


    std::cout  << "Your number is : " << num;

    return 0;
}
Mauricio Ruiz
  • 322
  • 2
  • 10