Aug 23, 2017 at 2:54pm Aug 23, 2017 at 2:54pm UTC
Project is Calculator. If user enters the input 1 + 1 = 2 and if user presses any key again, then console clears/refreshes and starts again like, enter the first value, second value, etc.. and repeats. How to do that? I don't want this command as system("CLS);, nty. It's evil :D
Aug 23, 2017 at 3:04pm Aug 23, 2017 at 3:04pm UTC
Declare const std::string clear( 100, '\n' ) ;
And then, to clear the screen: std::cout << clear ;
Aug 23, 2017 at 3:18pm Aug 23, 2017 at 3:18pm UTC
JLBorges, it works but I don't want it to clear the entire CONSOLE. I wanted it to refresh back to the point where user has to input a number to add/subtract, etc..
Aug 23, 2017 at 3:30pm Aug 23, 2017 at 3:30pm UTC
> I wanted it to refresh back to the point where user has to input a number to add/subtract, etc..
What does your program look like now?
Aug 23, 2017 at 3:38pm Aug 23, 2017 at 3:38pm UTC
#include <iostream>
#include <windows.h>
using namespace std;
int choice;
float num1, num2, result;
template <class T>
void get_number(T& choice)
{
while(!(cin >> choice))
{
cin.ignore(1000, '\n');
}
}
int main()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 3); cout << "CALCULATOR" << endl;
SetConsoleTextAttribute(h, 3); cout << "Made by Kristen\n\n" << endl;
SetConsoleTextAttribute(h, 7);
while(true)
{
cout << "\n1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Divide" << endl;
cout << "5. Exit\n" << endl;
cout << "What would you like to do? ";
get_number(choice);
if(choice == 5)
break;
if(choice < 1 || choice > 4)
continue;
cout << "\nPlease enter the first value: ";
get_number(num1);
cout << "Please enter the second value: ";
get_number(num2);
switch(choice)
{
case 1:
if(choice == 1)
result = num1 + num2;
case 2:
if(choice == 2)
result = num1 - num2;
case 3:
if(choice == 3)
result = num1 * num2;
case 4:
if(choice == 4)
result = num1 / num2;
}
cout << "\nThe result is: " << result << "\n";
SetConsoleTextAttribute(h, 2); cout << "\n\nThanks for using me!\n\n";
}
Aug 23, 2017 at 4:38pm Aug 23, 2017 at 4:38pm UTC
That is a good code, I never thought that I can do string clear :o. Thanks man. I learned something new. I tweaked my code a little and seriously.. I went for system :D.
#include <iostream>
#include <windows.h>
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif __cplusplus__
using namespace std;
int choice;
float num1, num2, result;
template <class T>
void get_number(T& choice)
{
while(!(cin >> choice))
{
cin.ignore(1000, '\n');
}
}
int main()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 3); cout << "CALCULATOR" << endl;
SetConsoleTextAttribute(h, 3); cout << "Made by Kristen Ungur\n\n" << endl;
SetConsoleTextAttribute(h, 7);
while(true)
{
cout << "\n1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Divide" << endl;
cout << "5. Exit\n" << endl;
cout << "What would you like to do? ";
get_number(choice);
if(choice == 5)
break;
if(choice < 1 || choice > 4)
continue;
cout << "\nPlease enter the first value: ";
get_number(num1);
cout << "Please enter the second value: ";
get_number(num2);
switch(choice)
{
case 1:
if(choice == 1)
result = num1 + num2;
case 2:
if(choice == 2)
result = num1 - num2;
case 3:
if(choice == 3)
result = num1 * num2;
case 4:
if(choice == 4)
result = num1 / num2;
}
cout << "\nThe result is: " << result << "\n";
cout << "\nEnter any character to continue: ";
char ch; cin >> ch;
if (system("CLS")) system("clear");
}
SetConsoleTextAttribute(h, 2); cout << "\n\nThanks for using me!\n\n";
}
This works also :3.
Aug 23, 2017 at 6:10pm Aug 23, 2017 at 6:10pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13
/* --- Function: void my_clearscrn(void) --- */
void my_clearscrn(void )
{
#ifdef __unix__
system("clear" );
#elif _WIN32
system("cls" );
#endif
}
Last edited on Aug 23, 2017 at 6:11pm Aug 23, 2017 at 6:11pm UTC