Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Small problem with my 'Menu program'

Everything looks okay except my multiplication table doesn't seem to be formatting properly EDIT i fixed my table, but my while loop at the end doesn't seem to be executing. So i could use some expertise :)
Code
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string calculatoropt;
    string multitableopt;
    string input;


    cout << " Please choose an option: "<< endl;

     cout    << " 1. Calculator" << endl;
      cout   << " 2. Multiplication Table" << endl;
         cin >> input;

         if( input =="1" )
         {
             int firstnumber;
             int secondnumber;


             cout << " Enter the first number: " << endl;
             cin >> firstnumber;
             cout << " Enter the second number: " << endl;
             cin >> secondnumber;

             cout << firstnumber << " * " << secondnumber << "=" << firstnumber * secondnumber << endl;
             cout << firstnumber << " + " << secondnumber << "=" << firstnumber + secondnumber << endl;
             cout << firstnumber << " / " << secondnumber << "=" << firstnumber / secondnumber << endl;
             cout << firstnumber << " - " << secondnumber << "=" << firstnumber - secondnumber << endl;

         }
         else if( input == "2" )
         {
             for (int i = 0; i < 10; i++)
             {
                 cout << '\t' << i;
             }
             cout << 'endl';

             for (int i = 0; i < 10; ++i)
             {
                 cout << i;

             for (int j = 0; j < 10; ++j)
             {
                 cout << '\t' << i * j << endl;
             }

             cout << endl;

             }

             while( input != "1" && input!= "2")
             {
                 cout << "Sorry, you've entered an invalid option" << endl;
                 cin >> input;
                 return 0;
             }


         }
}
What exactly is:
Code
cout << 'endl';

Do you read compiler messages? :S It should have been the first thing you do before posting a thread. Evidently you didn't. Btw, see any conflicting logic here?
Code
else if (input == "2")
{
   while (input != "1" && input != "2")
   {
   }
}

I think this should've been quite obvious that input will never be "1" in the while statement enclosed in an if statement branch where input is required to be "2" for that loop to even be evaluated. And if it is "2" (which it WILL BE), then the while loop won't run because of the last part after the &&.

Why can't you use a debugger if you can't see this? 🤨

Imagine writing:
Code
if (var == 1)
{
    while (var != 1)
   {
   }
}

^ That's what you're doing here.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
@dead10cc I see. I'm definitely having some logic conflicts here, i'm going to work on that more.


Code
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string calculatoropt;
    string multitableopt;
    string input;


    cout << " Please choose an option: "<< endl;

     cout    << " 1. Calculator" << endl;
      cout   << " 2. Multiplication Table" << endl;
         cin >> input;

         while (input !="1" && input !="2")
         {
             cout<< "Sorry, that option is invalid"<<endl;
             cin>> input;
         }

         if( input =="1" )
         {
             int firstnumber;
             int secondnumber;


             cout << " Enter the first number: " << endl;
             cin >> firstnumber;
             cout << " Enter the second number: " << endl;
             cin >> secondnumber;

             cout << firstnumber << " * " << secondnumber << "=" << firstnumber * secondnumber << endl;
             cout << firstnumber << " + " << secondnumber << "=" << firstnumber + secondnumber << endl;
             cout << firstnumber << " / " << secondnumber << "=" << firstnumber / secondnumber << endl;
             cout << firstnumber << " - " << secondnumber << "=" << firstnumber - secondnumber << endl;

}
         else if( input == "2" )
         {
             for (int i = 0; i < 10; i++)
             {
                 cout << '\t' << i;
             }


             for (int i = 0; i < 10; ++i)
             {
                 cout << i;

             for (int j = 0; j < 10; ++j)
             {
                 cout << '\t' << i * j;
             }
             }

         }


}
😎


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by Emre_Sep 3, 2019 09:41 PM 0 128 Sep 3, 2019 09:41 PM
Last Post: Emre_
[Shell] by Emre_Sep 1, 2019 07:17 PM 0 134 Sep 1, 2019 07:17 PM
Last Post: Emre_
[.NET] by _UnderDogOct 30, 2016 08:37 AM 5 267 Oct 30, 2016 08:37 AM
Last Post: Mr. Robot