I've always had this "thing" for the C programming language but because I was never able to validate user input with it, I went with C++ lol. Anyway, I've decided to give it another go, especially now that I am learning about the Win32 API and I want you guys to help me with something. I wrote this program in C++ (it is not completed yet but is a perfect example of what I want to accomplish in C) and I was wondering if someone could write a C version of it. Here's my program:
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
bool validate_number_of_names(const std::string &);
bool validate_name(const std::string &);
bool validate_sort_type(const std::string &);
bool length_and_spaces(const std::string &);
int main()
{
int number_of_names;
std::string user_input;
std::string name;
std::vector<std::string> all_names;
std::string sort_type;
do{
std::cout << "Number of names: ";
std::getline(std::cin, user_input);
}while(!validate_number_of_names(user_input));
system("cls");
std::istringstream iss(user_input);
iss >> number_of_names;
std::cout << "Now, start inputing the names. They have to be in the format\n"
<< "\"last, first, age\" with no spaces and the name string cannot\n"
<< "exceed 40 characters.\n\n";
while(number_of_names != 0){
do{
std::cout << "Name: (" << number_of_names << " left) ";
std::getline(std::cin, name);
}while(!validate_name(name));
all_names.push_back(name);
number_of_names--;
}
do{
std::cout << "\nThe names can be sorted by either last, first or age. How\n"
"would you like them to be sorted? ";
std::getline(std::cin, sort_type);
}while(!validate_sort_type(sort_type));
std::cin.get();
return 0;
}
bool validate_number_of_names(const std::string &user_input)
{
int number_of_names;
int length_of_user_input;
length_of_user_input = user_input.length();
for(int i = 0; i != length_of_user_input; i++){
if(!isdigit(user_input[i])){
std::cout << "Invalid entry.\n\n";
return false;
}
}
std::istringstream iss(user_input);
iss >> number_of_names;
if(number_of_names > 100){
std::cout << "Number of names must be between 1 and 100.\n\n";
return false;
}
return true;
}
bool validate_name(const std::string &name)
{
int i;
int comma;
int result;
int second_comma;
int comma_count = 0;
int number_of_characters;
std::string age;
std::string last_name;
std::string first_name;
std::string last_and_first_names;
if(!length_and_spaces(name))
return false;
number_of_characters = name.length();
for(i = 0; i != number_of_characters; i++){
if(name[i] == ','){
comma_count++;
}
}
if((comma_count < 2) || (comma_count > 2)){
std::cout << "Invalid entry.\n\n";
return false;
}
second_comma = name.find_first_of(',', name.find_first_of(',') + 1);
last_and_first_names = name.substr(0, second_comma);
comma = last_and_first_names.find_first_of(',');
last_name = last_and_first_names.substr(0, comma);
first_name = last_and_first_names.substr(comma + 1);
age = name.substr(name.rfind(',') + 1);
if(!length_and_spaces(last_name))
return false;
if(!length_and_spaces(first_name))
return false;
if(!length_and_spaces(age))
return false;
for(int i = 0; i != number_of_characters; i++){
if(last_and_first_names[i] != ','){
if(!isalpha(last_and_first_names[i])){
std::cout << "Invalid entry.\n\n";
return false;
}
return true;
}
}
return true;
}
bool validate_sort_type(const std::string &sort_type)
{
if((sort_type != "last") && (sort_type != "first") && (sort_type != "age")){
std::cout << "Invalid entry.\n";
return false;
}
return true;
}
bool length_and_spaces(const std::string &foo)
{
int number_of_characters;
number_of_characters = foo.length();
if(number_of_characters == 0){
std::cout << "Invalid entry.\n\n";
return false;
}
for(int i = 0; i != number_of_characters; i++){
if(isspace(foo[i])){
std::cout << "Invalid entry.\n\n";
return false;
}
}
return true;
}
Thanks.