C++ Validation Message -


i gets multiple time of error message after input multiple characters. example:"1300412". seem bring input loop, keep showing me "invalid input". how fix let show out "invalid input" once after wrong input entered.

here me code, please me.

void main(){ char option;  do{      system("cls");     cout << " select function: " << endl;     cout << "(a) asset depreciation." << endl;     cout << "(b) printing charge." << endl;     cout << "(c) packaging program." << endl;     cout << "(q) exit." << endl << endl;     cout << "please enter option:";     cin >> option;     option = toupper(option);     if (option == 'a'){         asset_depreciation();     }     else if (option == 'b'){         printing_charge();     }     else if (option == 'c'){         packaging_program();     }     else if (option == 'q'){         cout << "thanks using" << endl;         system("pause");     }     else{         cout << "invalid input!!! please enter again." << endl;         cin.clear();         cin.ignore();         system("pause");     } } while (option!='q');  } 

since option of type char can not hold more character. why error message.

if wish input string input.

std::string var; // accept input white space or \n occurred.  std::cin >> var; 

if wish to input line of string

std::string var; getline(std::cin,var); // accept input until \n has not entered. 

after comparison going

if (option == "a"){     asset_depreciation(); } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -