gets() has been removed from the C11 Standard
You can take
Code:
scanf("%19[^\n]", ten[i]);
This code is an example to tell 'scanf' for read all letter(including space) but not the Enter-key.
The number 19 after the percent sign limits the number of character wher read into
the string ten[i] to 19.
Exampe code for this:
Code:
#include <stdio.h>
#include <stdlib.h> //EXIT_SUCCESS
void clpuf(void)
{
while (getc(stdin) != '\n')
;
}
int main()
{
int i;
char c_posi[200], c_nega[200];
for (i = 0; i < 200; i++)
c_posi[i] = c_nega[i] = 0;
printf("Example positiv- and negativ list for scanf\n");
printf("\nPlease enter a Sting like 123430abc : ");
/** reads only characters where between the squere brackets
* ends the input by the first Letter wher is not in the list */
scanf("%[0123456789+-*/=]", c_posi);
printf("The string was: %s \n\n", c_posi);
/** cleans keyboar buffer */
clpuf();
printf("\nPlease enter a Sting like asdfgh123 : ");
scanf("%[^1234567890]", c_nega);
printf("Input was: %s \n\n", c_nega);
clpuf(); /** clear keyboard buffer */
printf("\nPlease enter a Sting with space-signs: ");
/** liest alle Zeichen ausser den Newline-Zeichen(Enter-Taste) ein*/
///scanf("%21[^\n]", c_nega); ---> wäre eine Begrenzung auf 21 Zeichen beim einlesen
scanf("%[^\n]", c_nega);
printf("Input was: %s \n\n", c_nega);
printf("End of Program\n");
return EXIT_SUCCESS;
}
Also i think it would be a better if you don*t define a function between an other.
Ty this:
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
void kiemtrachuoi(char chuoi[100]);
void checklogin(char user[100], int pass);
void sapxepTen(char ten[5][20]);
void nhapTen(char ten[5][20]);
void func1();
void func2();
void func3();
void clpuf(void);
int main(int argc, char **argv)
{
int choice;
do
{
printf("\t\tMENU");
printf("\n---------------------------------------\n");
printf("\n1. Count consonants and vowels in a string.");
printf("\n2. Login.");
printf("\n3. Sorting string array alphabetically.");
printf("\n4. Exit.");
printf("\n\n-------------------------------------\n");
printf("\nXin moi nhap vao lua chon cua ban: ");
scanf("%d", &choice);
clpuf();
switch(choice)
{
case 1: func1(); break;
case 2: func2(); break;
case 3: func3(); break;
case 4: exit(0); break;
default: printf("\nInvalid. Please choose again.\n");
}
} while(choice != 4);
return 0;
}
// Zaehlt im String Paramteter 1 die Vokale und Konsonanten
void kiemtrachuoi(char chuoi[100])
{
int i, consonant = 0, vowel = 0, wlen = 0;
wlen = strlen(chuoi);
for (i = 0; i < wlen; i++)
{
if(chuoi[i]== 'a' || chuoi[i]== 'i' || chuoi[i]== 'e' || chuoi[i]== 'u' || chuoi[i]== 'o' ||
chuoi[i]== 'A' || chuoi[i]== 'I' || chuoi[i]== 'E' || chuoi[i]== 'U' || chuoi[i]== 'O')
{
vowel++;
}
else
{
consonant++;
}
}
printf("\nString %s has %d vowels and %d consonants\n",chuoi,vowel,consonant);
return;
}
// Vergleicht Par1 mit internen String und Par2 mit interner Zahl
void checklogin(char user[100], int pass)
{
char userSys[] = "admin";
int passSys = 12345;
if(strcmp(user, userSys) == 0 && pass == passSys)
{
printf("\nLogin successful!!!\n");
}
else
{
printf("\nLogin failed!!!\n");
}
return;
}
void func1()
{
char chuoi[100];
printf("\nEnter a string: ");
scanf("%99[^\n]", chuoi);
kiemtrachuoi(chuoi); // Zaehlt im String Paramteter 1 die Vokale und Konsonanten
return;
}
// Enter username -> Enter password -> checklogin(user,pass) -> return
void func2()
{
char user[100];
int pass;
printf("Enter username: ");
scanf("%99[^\n]", user);
printf("\nEnter password: ");
scanf("%d", &pass);
checklogin(user,pass); // Vergleicht Par1 mit internen String und Par2 mit interner Zahl
return;
}
void func3()
{
char ten[5][20];
nhapTen(ten); // Speichert 5 Worte in Array von Parameter 1 (ten)
sapxepTen(ten);
printf("Set of Strings in Ascending alphabetical order: \n");
for (int i = 0; i < 5; i++)
{
printf("\nNumber %d is %s", i+1, ten[i]);
}
return;
}
void sapxepTen(char ten[5][20])
{
int i, j;
char temp[20];
for (i = 0; i < 4; i++) // 0 bis 3
{
for (j = i + 1 ; j < 5; j++) // 0 bis 4
{
if (strcmp(ten[i], ten[j]) > 0) // vergleicht 0-3 mit 0 bis 4 wenn par1 > par 2 strings tauschen
{
strcpy(temp, ten[i]);
strcpy(ten[i], ten[j]);
strcpy(ten[j], temp);
}
}
}
}
// Speichert 5 Worte in Array von Parameter 1
void nhapTen(char ten[5][20])
{
for (int i = 0; i < 5; i++)
{
printf("\nEnter 5 names from keyboard %d: ",i+1);
scanf("%19[^\n]", ten[i]);
clpuf();
}
}
void clpuf(void)
{
while (getc(stdin) != '\n')
;
}