logo elektroda
logo elektroda
X
logo elektroda

Handling Non-Numeric Input and Division by Zero in C++ Linear Equation Solver

mgpentium 20628 4
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 7031609
    mgpentium
    Level 16  
    Hello.
    So I wrote this program:


    int main()
    {
      float a;
      float b;
      float x;
    cout << "         licze rownanie liniowe, pierwszego stopnia, z jedna niewiadoma.\n";
    cout << "                                    ax+b=0\n";      
      cout << "Podaj a: ";
      cin >> a;
      cout << "Podaj b: ";
      cin >> b;
    
      if (a==0 && b==0) cout << "Nieskonczenie wiele rozwiazan\n";
      else if (a==0 && b!=0) cout << "Brak rozwiazan\n";
      x=-b/a;
      cout << "x = "<<endl;
    }
    

    The problem is that if I do not enter a number, but e.g. the letter L, some strange characters appear, and I would like it to show e.g. This is not a number.
    There is one more problem that if I give it 0 to a and 0 to b, it will show me that there are infinitely many solutions and also some other strange characters, and I would like these characters to be absent.

    Handling Non-Numeric Input and Division by Zero in C++ Linear Equation Solver



    Handling Non-Numeric Input and Division by Zero in C++ Linear Equation Solver

    Please place the code in code tags - krzychoocpp
  • ADVERTISEMENT
  • #2 7031668
    akartian
    Level 11  
    You declared the condition incorrectly, which means that the value of x always counts regardless of the event. You can do this:
    int main()
    {
    float int a;
    float int b;
    float int x;
    cout << " licze rownanie liniowe, pierwszego stopnia, z jedna niewiadoma.\n";
    cout << " ax+b=0\n";
    stat:
    cout << "Podaj a: ";
    cin >> a;
    cout << "Podaj b: ";
    cin >> b;
    
    if (a==0 && b==0) 
    {
    cout << "Nieskonczenie wiele rozwiazan\n";
    goto stat;
    }
    else if (a==0 && b!=0) 
    {
    cout << "Brak rozwiazan\n";
    goto stat;
    }
    x=-b/a;
    cout << "x = "<<endl; 


    The goto instruction is usually not used because the program is not very readable, but it can be done quickly.

    Please place the code in code tags - krzychoocpp
  • ADVERTISEMENT
  • #3 7031690
    krzychoocpp
    VIP Meritorious for electroda.pl
    My friend akartians , using goto makes no sense here. Why not use a while loop?

    This can be solved simply by adding else:
    
    if (a==0 && b==0) cout << "Nieskonczenie wiele rozwiazan\n"; 
    else if (a==0 && b!=0) cout << "Brak rozwiazan\n"; 
    else
    {
            x=-b/a; 
            cout << "x = "<<endl;
    }
    


    Checking whether the variable loading was successful:
    
    cout << "Podaj a: ";
    cin >> a;
            if(cin.fail())
                    cout << "Blad" << endl;
    


    Regards Krzysztof.
  • ADVERTISEMENT
  • #4 7031771
    mgpentium
    Level 16  
    krzychoocpp wrote:
    My friend akartians , using goto makes no sense here. Why not use a while loop?

    This can be solved simply by adding else:
    
    if (a==0 && b==0) cout << "Nieskonczenie wiele rozwiazan\n"; 
    else if (a==0 && b!=0) cout << "Brak rozwiazan\n"; 
    else
    {
            x=-b/a; 
            cout << "x = "<<endl;
    }
    


    Checking whether the variable loading was successful:
    
    cout << "Podaj a: ";
    cin >> a;
            if(cin.fail())
                    cout << "Blad" << endl;
    


    Regards Krzysztof.



    Where did you use the while loop?
    The first source code works, but the second one shows Error and some characters :D

    Added after 2 [minutes]:

    OK, I solved the problem, thank you all for your help.
  • #5 7031876
    krzychoocpp
    VIP Meritorious for electroda.pl
    I haven`t used it, but my friend wants an example, so here you go:
    
    while(true)
    {
      cout << "Podaj a: "; 
      cin >> a; 
      cout << "Podaj b: "; 
      cin >> b; 
    
      if (a==0 && b==0) cout << "Nieskonczenie wiele rozwiazan\n"; 
      else if (a==0 && b!=0) cout << "Brak rozwiazan\n";
      else 
      { 
            x=-b/a; 
            cout << "x = "<<endl; 
            break;
      }
    }
    


    The program will ask for the parameters of the equations until there is one solution.
ADVERTISEMENT