Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:04:17+00:00 2026-06-13T19:04:17+00:00

Possible Duplicate: How to determine if a string is a number with C++? I

  • 0

Possible Duplicate:
How to determine if a string is a number with C++?

I have written a very simple calculator program in C++. Here it is:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   double num1;
   double num2;
   string op;
   double num3;
   int x;
   bool y = false;

   do
   {
      cout<<"Press t to terminate the application"<<endl;

      cout<<"Enter the first number"<<endl;
      cin>>num1;

      cout<<"Enter the operator"<<endl;
      cin>>op;
      cout<<"Enter the next number"<<endl;
      cin>>num2;

      if(op=="/"&&num2==0)
      {
         cout<<"You are attempting to divide by 0. This is impossible and causes the destruction of the universe. However, the answer is infinity"<<endl;
         y = true;
      }

      if(y==false)
      {
         if(op=="+") {
            num3 = num1+num2;
         }
         else if(op=="-") {
            num3 = num1-num2;
         }
         else if(op=="*"||op=="x"||op=="X") {
            num3 = num1*num2;
         }
         else {
            num3 = num1/num2;
         }
         cout<<endl;
         cout<<endl;
         cout<<"Answer:"<<num3<<endl<<endl;
      }
   } while(x!=12);

   return 0;
}

As you can see, I want to allow people to terminate the application by pressing ‘t’. This obviously won’t work because cin will try and assign a letter to a double (if I do press ‘t’ the application crashes). I am planning to use strings instead to get the input, but how would I test if the string is a letter or a number?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:04:20+00:00Added an answer on June 13, 2026 at 7:04 pm

    Here is sample and working code, just change it so it suits your needs

    #include <iostream>
    #include <string>
    #include <cctype>
    #include <stdlib.h>
    
    using namespace std;
    
    bool isNum(char *s) {
        int i = 0,  flag;
    
        while(s[i]){
                //if there is a letter in a string then string is not a number
            if(isalpha(s[i])){
                flag = 0;
                break;
            }
            else flag = 1;
            i++;
            }
        if (flag == 1) return true;
        else return false;
    }
    
    
    int main(){
        char stingnum1[80], stringnum2[80];
        double doublenum1, doublenum2;
        cin>>stingnum1>>stringnum2;
        if(isNum(stingnum1) && isNum(stringnum2)){
            doublenum1 = atof(stingnum1);
            doublenum2 = atof(stringnum2);
            cout<<doublenum1 + doublenum2 << endl;
        } 
        else cout<<"error";
    
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Determine Number of Pages in a PDF File using C# (.NET 2.0)
Possible Duplicate: Simple way to determine if string is only characters, or to check
Possible Duplicate: How to determine if a number is a prime with regex? Here
Possible Duplicate: Programming java to determine a symmetrical word am new here, but I
Possible Duplicate: Determine a string's encoding in C# Many text editorsr (like Notepad++) can
Possible Duplicate: How to determine the class of a generic type? I have a
Possible Duplicate: Php function to determine if a string consist of only alphanumerical characters?
Possible Duplicate: Determine framework (CLR) version of assembly I have a library/DLL file which
Possible Duplicate: Determine a string's encoding in C# I believe if I create a
Possible Duplicate: Using Case/Switch and GetType to determine the object If I hope to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.