I have a program to write: write a program where user enters numbers until he enters 'e' or 'E'. All number entered by user sum and write on console.
So far, I write these, but I don't know how to break the loop when 'e' or 'E' is enter!? So, if anyone can help me with this.
#include<iostream>
using namespace std;
void main()
{
int sum=0, number=0;
do
{
cout << "Enter a number: \n";
cin >> number;
sum+=number;
} while (don't know what to type here);
cout << "Sum = " << sum << endl;
}
So try this piece of code please.
#include<iostream>
using namespace std;
int main()
{
int sum = 0, number = 0;
while (true)
{
cout << "Enter a number: \n";
if (cin >> number) sum += number;
else break;
}
cin.clear(); //reset the state of cin
char ch;
cin >> ch;
if (!(ch == 'e' || ch == 'E'))
{
cout << "Invalid input!" << endl;
system("pause");
return 0;
}
cout << "Sum = " << sum << endl;
}
you should make your program accept string as input, then try to convert that string to integer.
#include<iostream>
using namespace std;
int main() {
int sum = 0;
string number;
while (true){
cout << "Enter a number: ";
cin >> number;
if (number.length() == 1 && (number.at(0) == 'E' || number.at(0) == 'e')) break;
const char* data = number.c_str();
char* end = (char*)data + number.length();
int value = strtol(data, &end, 10);
sum += value;
};
cout << "Sum = " << sum << endl;
return 0;
}
Related
So im having a problem with rejecting decimals, Im rejecting any characters but when it comes to decimals it just seems not to be working. ((a % 1) != 0) was the equation i used
Also, I'm having a problem with the do loop, when the user inputs 'y' or 'Y' it seems to double the last value that was recorded from the previous test. I'm a freshman just trying to learn more so please help me out, guys.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int whatItDoes(int* a, int* b)
{
int temp = *a;
*a = *b * 10;
*b = temp * 10;
return *a + *b;
}
int main()
{
int a;
int b;
bool input1 = false;
bool input2 = false;
double temp;
char again;
cout << "------------------" << endl;
cout << "solovesa WELCOME " << endl;
cout << "------------------" << endl;
do
{
while (!input1) //Validation
{
cout << "First integer: ";
string line;
cin >> line;
istringstream is(line);
char dummy = '\0'; //null termination char marks end of string
if (!(is >> a) || (is >> ws && is.get(dummy))) //ws meaning
{
cout << " " << endl;
cout << "WARNING: Characters are not allowed" << endl;
cout << " " << endl;
}
else
input1 = true;
if ((a % 1) != 0)
{
cout << " " << endl;
cout << "WARNING: Decimals are not allowed" << endl;
cout << " " << endl;
}
else
input1 = true;
}
while (!input2) //Validation
{
cout << "Second integer: ";
string line;
cin >> line;
istringstream is(line);
char dummy = '\0'; //null termination char marksd end of string
if (!(is >> b) || (is >> ws && is.get(dummy))) //ws meaning
{
cout << "Characters are not allowed" << endl;;
}
else
input2 = true;
}
cout << " " << endl;
whatItDoes(&a, &b);
cout << "Final: " << (a + b) << endl;
cout << " " << endl;
cin.clear();
//repeat program prompt
cout << "Would u like to do another set ? (Y/N)"; //ask user if they want to repeat
cin >> again;
//if they say no then exits program but if yes it repeats to top program
}
while (again == 'Y' || again == 'y');
system("pause");
return 0;
}
``````
Recommendation:
pass a and b by ref.
int whatItDoes(int* a, int* b) --> int whatItDoes(int& a, int& b)
Your Question:
if I understand your code correctly - an ascii character '.' translates to the numeric value of 46 (google Ascii table). So, 46 % 1 == 0.
You can explicitly check for the decimal, if( (a % 46) == 0 ), but be careful, as 0 % 46 == 0 (but character zero ('0') has a numeric value of 48 ).
Consider what I mentioned about the ASCII table and the numeric values of a char.
I have created an array that holds 5 numbers, and the user inputs the numbers. If the mark is less than 0 and greater than 100, I want to print out "invalid mark number". How could I do that?
using namespace std;
int mark[5];
int main ()
{
cout << "enter mark 0: ";
cin >> mark[0];
cout << "enter mark 1: ";
cin >> mark[1];
cout << "enter mark 2: ";
cin >> mark[2];
cout << "enter mark 3: ";
cin >> mark[3];
cout << "enter mark 4: ";
cin >> mark[4];
}
You should use a for-loop to make the code more readable and compact. Because once you introduce if statements, the code size would grow alot. It should look like this:
#include <iostream>
using namespace std;
int mark[5];
int main () {
for (int i = 0; i < 5; i++){
cout << "enter mark " << i << ": ";
cin >> mark[i];
if (mark[i] < 0 || mark[i] > 100){
cout << "invalid mark number\n";
}
}
}
Don't use using namespace std; (read here why) and keep the int mark[5]; inside the main-function (read here why). Also to add to the logic force the user to input again:
#include <iostream>
int main () {
int mark[5];
for (int i = 0; i < 5; i++){
bool valid_input = false;
while (!valid_input){
std::cout << "enter mark " << i << ": ";
std::cin >> mark[i];
if (mark[i] < 0 || mark[i] > 100){
std::cout << "invalid mark number\n";
}
else{
valid_input = true;
}
}
}
}
I'm trying to display "invalid option" when the user enters a letter instead of a number. I tried using the isalpha() function but I get an infinite loop showing 0 Invalid option!try again:. The 0 from the output is displayed when entering a letter. When I actually type in the number 0 the message is displayed and the loop is exited.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;
int main() {
// Vaules do not change
const int minNum = 1;
const int maxNum = 100;
int userInput;
// Changes every second
srand(time(0));
// Random number is stored
const int answer = minNum + (rand() % maxNum);
cout << answer << endl; // For testing purposes
cout << "Guess a number 1-100: ";
cin >> userInput;
cout << endl;
if(userInput == answer) {
cout << "Correct!\n\n";
}
while(userInput != answer) {
if(userInput < 1 || userInput > 100 || isalpha(userInput)) {
cout << userInput << " Invalid option!\ntry again: ";
cin >> userInput;
cout << endl;
}
else if(userInput < answer) {
cout << userInput << " is too low!\ntry again: ";
cin >> userInput;
cout << endl;
}
else if(userInput > answer) {
cout << userInput << " is too high!\ntry again: ";
cin >> userInput;
cout << endl;
}
}
cout << userInput << " is correct!\n\n";
return 0;
}
When you need to deal with user input differently based on some logic, your best option is to:
Read lines of text. Figure out what to do when there is no more input.
Process each line of text using custom logic.
In your case, you could use:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;
int main() {
// Vaules do not change
const int minNum = 1;
const int maxNum = 100;
int userInput;
// Changes every second
srand(time(0));
// Random number is stored
const int answer = minNum + (rand() % maxNum);
cout << answer << endl; // For testing purposes
std::string line;
cout << "Guess a number 1-100: ";
while ( getline(std:::cout, line ) )
{
// Deal with empty lines.
if ( line.size() == 0 )
{
continue;
}
// If the first character is a letter ...
if(isalpha(line[0])) {
cout << line << "\n Invalid option!\ntry again: ";
continue;
}
// Extract the number from the line using a stringstream.
// If there is a problem extracting the number ...
std::istringstream str(line);
if ( !(str >> userInput ) )
{
cout << line << "\n Invalid option!\ntry again: ";
continue;
}
cout << endl;
// Check the user input against the random answer.
if(userInput == answer) {
cout << "Correct!\n\n";
}
else if(userInput < 1 || userInput > 100 ) {
cout << userInput << " Invalid option!\ntry again: ";
}
else if(userInput < answer) {
cout << userInput << " is too low!\ntry again: ";
}
else if(userInput > answer) {
cout << userInput << " is too high!\ntry again: ";
}
cout << "Guess a number 1-100: ";
}
cout << userInput << " is correct!\n\n";
return 0;
}
I'm new into programing. I'm working on a program that will tell the odd and even numbers that are less or equal to the user's input (for inputs >=0). How do I make the program run if value is >=0 and ask for the input again if the value is <0.
Here is the program:
> #include <iostream>
using namespace std;
int main() {
cout << "Please enter only a positive value\n";
// Get number from user
int input = 0;
cout << "Enter a number:\n";
cin >> input;
int number = 1;
while (input >= 0 && number <= input) {
// Print some numbers
cout << "Here are some odd numbers:\n";
while (number <= input) {
cout << number << endl;
number = number + 2;
}
// Print some numbers
cout << "Here are some even numbers:\n";
int number2 = 0;
while (number2 <= input) {
cout << number2 << endl;
number2 = number2 + 2;
}
}
return 0;
}
Correct code:
#include <iostream>
using namespace std;
int main() {
cout << "Please enter only a positive value\n";
// Get number from user
int input = 0;
cout << "Enter a number:\n";
cin >> input;
while (input < 0) {
cout << "Input is negative. Enter again: \n";
cin >> input;
}
int number = 1;
while (input >= 0 && number <= input) {
// Print some numbers
cout << "Here are some odd numbers:\n";
while (number <= input) {
cout << number << endl;
number = number + 2;
}
// Print some numbers
cout << "Here are some even numbers:\n";
int number2 = 0;
while (number2 <= input) {
cout << number2 << endl;
number2 = number2 + 2;
}
}
return 0;
}
Note that I added this chunk:
while (input < 0) {
cout << "Input is negative. Enter again: \n";
cin >> input;
}
to handle negative inputs.
while (input < 0)
{
cout << "Enter a number:\n";
cin >> input;
if (input < 0)
// Here you can print something to tell that input was not correct
}
Hi here is my question.
Write a program that uses a do-while statement. It reads in an integer
n from the keyboard. If n is not in the range 1 to 10 it makes an audible
“beep” and asks for another integer. If n is in the correct range, it writes
out “You have input n” and then exits.
Here is my answer.
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
int number = 0;
do
{
cout << "Enter an integer." << endl;
cin >> number;
if (!(number >= 1 && number <= 10))
{
Beep(400, 400);
}
}
while (!(number >= 1 && number <= 10));
cout << "You have input " << number << endl;
system("PAUSE");
}
You can see the line
(!(number >= 1 && number <= 10))
is repeated. Is there any workaround for this?
int GetNumber()
{
int number;
cout << "Enter an integer." << endl;
cin >> number;
return number;
}
void main()
{
int n = GetNumber();
while(n < 1 || n > 10)
{
Beep(400, 400);
n = GetNumber();
}
cout << "You have input " << n << endl;
system("PAUSE");
}
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
int number = 0;
bool invalid_input = true;
do
{
cout << "Enter an integer." << endl;
cin >> number;
invalid_input = !(number >= 1 && number <= 10);
if (invalid_input)
{
Beep(400, 400);
}
}
while (invalid_input);
cout << "You have input " << number << endl;
system("PAUSE");
}
Perhaps...
int number = 0;
while (true)
{
cout << "Enter an integer." << endl;
if (cin >> number && number >= 1 && number <= 10)
break;
Beep(400, 400);
}
cout << "You have input " << number << endl;
system("PAUSE");