//Calculator Revision to say error when invalid character

#include <iostream>
using namespace std;

int main()
{
    cout <<"Welcome to the 1.1 calculator by Niklas Kunkel";
    cout << "\nPlease enter 2 numbers and press enter after each number";
    
    float num1;
    float num2;
    char op;
    float result;
    char again = 'y';
    while (again == 'y')
    
    {
    cout <<"\n Please enter the first number: ";
    cin >> num1;
    cout <<"\n num 1 = " << num1 << endl;
    
    if (num1//This is where im stuck I am trying to have it say "if num1 is a # do the following code but skip the else part
        {   
                cin >> num2;
                cout << "num2 = " << num2 << endl;
                if (num2 //Im stuck here as well with the same problem as above. If num2 is a # skip else and do the rest of the code
                 
                   {  cout <<"\n Now enter an operation symbol (+ - / *)";
                      cin >> op;
                               if (op == '+')
                                       cout << "\n num1 + num2 = " << num1 + num2 << endl;
                               if (op == '-')
                                       cout <<"\n num1 - num2 = " << num1 - num2 << endl;
                               if (op == '*')
                                       cout <<"\n num1 * num2 = " << num1 * num2 << endl;
                               if (op == '/')
                                       cout << " \n num1 / num2 = " << num1 / num2 << endl;                                                     
                               else
                                    {cout << "Invalid Operation";
                                     cout << "\n Do you want to try again? (y/n): ";
                                     cin >> again;
                                    
                  }
            else 
                  {
                      cout <<"\n Error: Item Entered is not a number";
                      cout <<"\n Do you want to try again? (y/n): ";
                      cin >> again;
                  }
            
        } 
    else
    {   
        cout<< "Error: Item entered is not a number";
    
        cout << "\n Do you want to try again? (y/n): ";
        cin >> again;
    }
    
     return 0;
}

I know its really hard to read. I had to nest a lot of if statements together and I know there must be an easier way to make it look less cluttered. I'm having a problem where I am trying to tell the program "if varaible num1 is a # do the following code if it isnt a number go to else which will say "invalid character".

Other than that I think I might quite a few mistakes with the nesting to. Ive heard of something called switch blocks but I have no idea how to use them.

I know its really hard to read. I had to nest a lot of if statements together and I know there must be an easier way to make it look less cluttered. I'm having a problem where I am trying to tell the program "if varaible num1 is a # do the following code if it isnt a number go to else which will say "invalid character".

each extract a field and convert it to a numeric value by calling use_facet<num_get<Elem, InIt>(getloc()). get(InIt( rdbuf()), Init(0), *this, getloc(), val). Here, InIt is defined as istreambuf_iterator<Elem, Tr>, and val has type long, unsigned long, or void * as needed.

If the converted value cannot be represented as the type of val, the function calls setstate(failbit). In any case, the function returns *this.

So check cin.good() after operation>> I guess...

Other than that I think I might quite a few mistakes with the nesting to. Ive heard of something called switch blocks but I have no idea how to use them.

Appropriate nesting is not a c++ question IMO (atleast I am not going to bother checking it manually)
switch syntax can be found in ANY java/c/c++ (insert related language here)...
example

switch( integer_value )
{
constant_integer_value:
  instruction block ;
  break ;
  // ...
default:
}

(lol, up to 4 edits now, Im starting to feel like a design dork)

5...

Whats up with the Quoted by Niklas... Isn't it I who quote nicklas ? odd

I'm sorry I'm really new to C++ and although your concept may be right (just like all the other books) the problem I have is how to use them in my programs.

See This...ur basic problem was not using ')' and '}' at many places...There are better ways to do this program...like using switch...u should read a bit about that in ur book or search for any beginners tutorials

#include <iostream>
using namespace std;

int main()
{
    cout <<"Welcome to the 1.1 calculator by Niklas Kunkel";
    cout << "\nPlease enter 2 numbers and press enter after each number";
    
    float num1;
    float num2;
    char op;
    float result;
    char again = 'y';
    while (again == 'y')
    
    {
    cout <<"\n Please enter the first number: ";
    cin >> num1;
    cout <<"\n num 1 = " << num1 << endl;
    
    if (num1)//This is where im stuck I am trying to have it say "if num1 is a # do the following code but skip the else part
        {   
                cin >> num2;
                cout << "num2 = " << num2 << endl;
                if (num2) //Im stuck here as well with the same problem as above. If num2 is a # skip else and do the rest of the code
                 
                   {  cout <<"\n Now enter an operation symbol (+ - / *)";
                      cin >> op;
                               if (op == '+')
                                       cout << "\n num1 + num2 = " << num1 + num2 << endl;
                               if (op == '-')
                                       cout <<"\n num1 - num2 = " << num1 - num2 << endl;
                               if (op == '*')
                                       cout <<"\n num1 * num2 = " << num1 * num2 << endl;
                               if (op == '/')
                                       cout << " \n num1 / num2 = " << num1 / num2 << endl;                                                     
                               else
                                    {
									 cout << "Invalid Operation";
                                     cout << "\n Do you want to try again? (y/n): ";
                                     cin >> again;
                                    
					                 }
				}
            else 
                  {
                      cout <<"\n Error: Item Entered is not a number";
                      cout <<"\n Do you want to try again? (y/n): ";
                      cin >> again;
                  }
            
        } 
    else
    {   
        cout<< "Error: Item entered is not a number";
    
        cout << "\n Do you want to try again? (y/n): ";
        cin >> again;
    }
    
	}
	return 0;
}

Why are u declaring variables if u are not using them like result

Sorry but I was using a copy of an earlier program and trying to edit it. Do you mind explaining what I would do here

if (num1)//This is where im stuck I am trying to have it say "if num1 is a # do the following code but skip the else part
      
  {   
                cin >> num2;
                cout << "num2 = " << num2 << endl;
                if (num2) //Im stuck here as well with the same 
                problem as above. If num2 is a # skip else and do             the rest of the code
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.