user invalid input
Mar 26, 2023 at 9:14am UTC
hey guys im trying to make the user to loop only with the option of y/n only if ever the user pressed a number or another letter it would say invalid only choose between y/n
ln 123 to 131
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#include <iostream>
using namespace std;
void singlestring(){ //array()
string name[5];
cout << "Enter 5 Names: " ;
for (int i = 0; i < 5; i++){
cin >> name[i];
}
for (int g = 0; g < 5; g++){
cout << name[g];
}
}
void singleint(){ //array()
int num [3];
cout << "Enter three numbers: " ;
for (int i = 0; i < 3; i++){
cin >> num[i];
}
cout << "The Numbers are: " ;
for (int g = 0; g < 3; g++){
cout << num[g];
}
}
void concatonate(){
string name[4];
cout << "Enter your First Name: " ;
cin >> name[0];
cout << "Enter your Middle Name: " ;
cin >> name[1];
cout << "Enter your Surname: " ;
cin >> name[2];
name[3] = name[0] + " " + name[1] + " " + name[2];
cout << name[3];
}
void lenght(){
string word;
cout << "Enter a word " ;
while (!(cin >> word)){
cout << "Integers are Invalid: " ;
cin .clear();
cin.ignore(100 , '\n' );
}
cout << "Word: \n" << word << endl;
cout << "contains:" << word.length();
}
void changeletter(){
char first[100],second;
cout << "Enter a string: " ;
cin >> first;
cout << "Change the First letter: " ;
cin >> second;
first[0] = second;
cout << first;
}
void array(){
char choice;
cout << "A.Single or B.Multi\n" ;
cin >> choice;
while (!(choice == 'a' || choice == 'A' ) && (choice == 'b' || choice == 'B' )){
cout << "Must choose only the choices above: " ;
cin >> choice;
cin.clear();
cin.ignore(100 , '\n' );
}
if (choice == 'a' || choice == 'A' ){
char chc;
cout << "A.Integer or B.String: " ;
cin >> chc;
while (!(chc == 'a' || chc == 'A' ) && (chc == 'b' || chc == 'B' )){
cout << "Must choose only the choices above: " ;
cin >> chc;
cin.clear();
cin.ignore(100,'\n' );
}
if (chc == 'a' || chc == 'A' ){
singleint();
}else if (chc == 'b' || chc == 'B' ){
singlestring();
}
}
}
int main (){
cout << "All CT-203\n" ;
char choice;
do {
char opt;
cout << "A.Concatonate\n" ;
cout << "B.Lenghts of a string\n" ;
cout << "C.Changing a Letter \n" ;
cout << "D.Array\n" ;
cin >> opt;
while (!(opt == 'a' || opt == 'A' ) && (opt == 'B' || opt == 'B' ) && (opt == 'c' || opt == 'C' ) && (opt == 'd' || opt == 'D' )){
cout << "Only enter in the options above: " ;
cin.clear();
cin.ignore(100, '\n' );
cin >> opt;
}
if (opt == 'A' || opt == 'a' ){
concatonate();
}else if (opt == 'B' || opt == 'b' ){
lenght();
}
cout << "\nWould you like to make another transaction? " ;
cin >> choice;
while (!(choice == 'y' || choice == 'Y' ) && (choice == 'n' || choice == 'N' )){
cout << "Must be only in the choices above\n Try again: " ;
cin.clear();
cin.ignore(100, '\n' );
cin >> choice;
}
}while ((choice == 'y' || choice == 'Y' ) && (choice == 'n' || choice == 'N' ));
}
Mar 26, 2023 at 9:22am UTC
The logic on L125 is not right. Consider:
while (!(choice == 'y' || choice == 'Y' || choice == 'n' || choice == 'N' )) {
What is L131 supposed to do? Do you mean:
} while (choice == 'y' || choice == 'Y' );
Mar 26, 2023 at 9:29am UTC
now it works thank you
Topic archived. No new replies allowed.