HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Polynomial program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
programpolynomialstackoverflow

Problem

The program basically takes a polynomial and does some simple calculations with it.

With a couple of hours of work I managed to get my first console application to work (aside from the obligatory "Hello, World!", of course). However, I'm in total doubt that the style and habits I've used are even close to being correct.

```
#include
#include
#include
#include
#include
#include

using namespace std;

class Polynomial {
float a, b, c, value;

public:
Polynomial ();
void functionDefine(Polynomial poly);
void functionValue(Polynomial poly);
void functionZero(Polynomial poly);
};

namespace Utils {
void mainMenu(Polynomial poly);
void skipToMenu();
string save(string str);
int exitProgram();

void mainMenu(Polynomial poly) {
int choice = 0;

cout > choice;
cout nul");

}

string save(string str) {
string saved = "Yes or no"; // To do.
return saved;
}

int exitProgram() {
return 0;
}
}

Polynomial::Polynomial() {
a = 0;
b = 0;
c = 0;

}

void Polynomial::functionDefine (Polynomial poly) {
char currentChar = 'a';

cout > poly.a; break;
case 1:
cin >> poly.b; break;
case 2:
cin >> poly.c; break;
}

if (!cin) {
cout (currentChar + 1);

}

cout > input;

value = poly.a pow(input, 2) + poly.b input + poly.c;
cout 0) {

float result = ((-poly.b) + sqrt(abs(d))) / (2 * poly.a);
float secondResult = (-poly.b - sqrt(abs(d))) / (2 * poly.a);
cout << "The polynomials roots are at x = " << result << " and x = " << secondResult << "." << endl << endl;
}

else if (d == 0) {
float result = ((-b) + sqrt(abs(d))) / (2 * a);
cout << "The polynomials root is at x = " << result << "." << endl << endl;
}

else {
cout << "The polynomial has no roots." << endl << endl;
}

Utils::mainMe

Solution

First, your program has no need for these Libraries:

#include 
#include 
#include 


The is redundant due to the use of . is the C++ way. Another example is instead of . The C libraries are deprecated in C++, mainly they're just for compatibility. is for file streams, your program doesn't use any.

Secondly, I would avoid using system("pause"), it is EXTREMELY resource heavy compared to what it does. There are many ways to implement a pause into your program, the easiest however do not allow you to press any key to continue, you have to generate a new line to continue (Press Enter). If you include the library you can do something like the following:

#include 
#include 

int main()
{
    int test;

    std::cout > test;

    std::cin.clear();
    std::cin.ignore(std::numeric_limits::max(),'\n');
    std::cin.get();
    return 0;
}


Make sure you pay attention to these lines:

std::cin.clear();
std::cin.ignore(std::numeric_limits::max(),'\n');
std::cin.get();


The first 2 lines clear the buffer, which is important. Without those lines there is still a '\n' character in the buffer from the previous input. When your program reaches:

std::cin.get();


It will read that '\n' character and then continue. By clearing the buffer first, the program will get input until it receives a '\n' from the user by pressing enter.

I usually create a void function out of the first 2 lines.

Lastly, you should read this: Why is “using namespace std;” considered bad practice?

Hope that helps a bit.

Code Snippets

#include <fstream>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <limits>

int main()
{
    int test;

    std::cout << "Var: ";
    std::cin >> test;

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    std::cin.get();
    return 0;
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cin.get();
std::cin.get();

Context

StackExchange Code Review Q#26817, answer score: 4

Revisions (0)

No revisions yet.