I cant get the functions 2 and 3 to work. Please advise on what I need to do. Pr
ID: 441094 • Letter: I
Question
I cant get the functions 2 and 3 to work. Please advise on what I need to do. Program is attached below This lab focuses on the manipulation of c-strings. The user will be prompted to enter a word. They will then be given a menu with the following options: 1. Pig Latin 2. Count Vowels & Consonants 3. Quit Menu Item 1: Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form pig-Latin phrases. For simplicity, use the following algorithm: 1. Move the first letter of the word to the end 2. Add the lettersExplanation / Answer
One request Plz don't rate those who try to copy and paste this post in their answer.
This is the Corrected answer for your program..It run fine ..Fully working.Check it out.
misteks in your program are :
1 :- In ur program u try to get input from keyboard using getchar() function.do it using simple scanf() .it will work.
2 :- By Using scanf("%d",&Selection); you dont have to for validation method as it will take interger value of what ever u input.So anything else than 1,2,3 will result invalid input.
3 :- In validation method u have used atoi function with value atoi(*int) but it requires atoi(*char).any way it does required for this pragram if u use scanf().
Corrected program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void Menu(void);
void ValidationControl(int *Selection);
void PigLatin(char String[]);
void CharacterCount(char String[], int *TotalCount, int *VowelCount);
int main(void)
{
int Selection = 0;
while(Selection != 3)
{
do
{
Menu();
scanf("%d",&Selection);
char Trash[2];
fgets(Trash, sizeof(Trash), stdin);
ValidationControl(&Selection);
} while(Selection == -1);
switch(Selection)
{
case 1:
{
char String[37];
memset(String, 0, sizeof(String));
printf("Please enter a word (up to 34 characters in length): ");
fflush(stdout); fgets(String, 35, stdin);
printf(" "); PigLatin(String);
break;
}
case 2:
{
char String[100];
int TotalCount = 0;
int VowelCount = 0;
memset(String, 0, sizeof(String));
printf ("Enter a sentence: ");
fgets(String, 100, stdin);
CharacterCount(String, &TotalCount, &VowelCount);
printf("The total number of characters is: %d ", TotalCount);
TotalCount = TotalCount - VowelCount;
printf("You entered: %d total consonants and %d total vowels ", TotalCount, VowelCount);
break;
}
}
}
return 0;
}
void Menu(void)
{
printf("Please select one of the following (you may Quit from the main menu at any time): ");
printf("1.Pig Latin ");
printf("2.Count Vowels & Consonants ");
printf("3.Quit ");
}
void ValidationControl(int *Selection) {
/*
int ValidSelection = 0;
if((isdigit(*Selection) != 0))
{
ValidSelection = *Selection;
printf("%d",ValidSelection);
// char Trash[2];
// fgets(Trash, sizeof(Trash), stdin);
if(ValidSelection != 1 && ValidSelection != 2 && ValidSelection != 3)
{
printf("Line 190: Invalid selection. ");
*Selection = -1;
}
else if(ValidSelection == 1 || ValidSelection == 2 || ValidSelection == 3)
{
printf("%d",ValidSelection);
if(ValidSelection != 3)
{
*Selection = 0;
}
else if(ValidSelection == 1)
{
*Selection = 1;
}
else if(ValidSelection == 2)
{
*Selection = 2;
}
else
{
*Selection = 3;
}
}
}
else
{
printf("Line 220 : Invalid selection. ") ;
}*/
}
void PigLatin(char String[])
{
int i;
for(i = 0; i < 35; ++i)
{
if(String[i] == '')
{
String[i - 1] = String[0];
String[i] = 'a';
String[i + 1] = 'y';
String[i + 2] = ' ';
break;
}
}
i = 1;
while(String[i] != '')
{
String[i - 1] = String[i];
String[i] = '';
++i;
}
printf("The Pig Latin equvalent of the word you entered is: %s ", String);
}
void CharacterCount(char String[], int *TotalCount, int *VowelCount)
{
const char Vowels[11] = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', ''};
char Store; int i, j;
for(i = 0; String[i] != ''; ++i)
{
if(isalpha(String[i]) != 0) {
++*TotalCount;
}
}
for(i = 0; Vowels[i] != ''; ++i)
{
Store = Vowels[i];
for(j = 0; String[j] != ''; ++j)
{
if(Store == String[j])
{
++*VowelCount;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.