1
0

Compare commits

...

5 Commits

Author SHA1 Message Date
a9b58871c0
Test loop and input error checking 2.
Fix issue with error checking.
2022-08-26 12:10:43 +01:00
9c7d7772e6
Test loop and input error checking 1. 2022-08-26 11:55:54 +01:00
fffcdedae3
Test static duration of local variables. 2022-08-18 11:59:26 +01:00
21b7b2bc1c
Lesson 5.6.
approximatelyEqual for double / float.
2022-08-15 16:13:57 +01:00
9d2f0822c3
Exponentiation By Squaring. 2022-08-15 15:08:54 +01:00

View File

@ -1,21 +1,60 @@
#include <iostream>
int getValue()
{
std::cout << "Enter an integer: ";
int x{};
std::cin >> x;
return x;
}
using string = std::string;
void ignoreTillLineEnd();
string promptfs(string);
int promptfi(string);
int main()
{
int a{ getValue() };
int b{ getValue() };
int c{ getValue() };
std::cout << a + (b * c); // order of eval doesn't matter now
int nameC {promptfi("Enter the number of names:")};
if (nameC < 1) {
std::cout << "No names to be entered!\n";
return 1;
}
string* names = new string[nameC];
for (int i = 0; i < nameC; i++) names[i] = promptfs("Enter your name:");
std::cout << "I have " << nameC << " to say hello to!\n";
for (int i = 0; i < nameC; i++) std::cout << "Hello " << names[i] << ".\n";
return 0;
}
void ignoreTillLineEnd() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
string promptfs(string msg) {
bool once {false};
string toret {};
do {
if (once) {
std::cin.clear();
ignoreTillLineEnd();
std::cout << "Input Failure!\n";
}
std::cout << msg << "\n";
std::cin >> toret;
once = true;
}
while (std::cin.fail());
ignoreTillLineEnd();
return toret;
}
int promptfi(string msg) {
bool once {false};
int toret {};
do {
if (once) {
std::cin.clear();
ignoreTillLineEnd();
std::cout << "Input Failure!\n";
}
std::cout << msg << "\n";
std::cin >> toret;
once = true;
}
while (std::cin.fail());
ignoreTillLineEnd();
return toret;
}