SYNTAX AND SEMANTICS

P

Prof.Stanley

HELLO ALL I AM TRYING TO PUT UP A PROGRAM THAT DETECTS DOUBLE VOWELS
AFTER RECIEVING AN INPUT STRING; MY MAIN PROBLEM LIES IN THE INPUT CAN
SOMEBODY KINDLY HELP DETECT ANY SYNTAX OR SEMANTIC ERRORS:

HERE IS THE CODE:



#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void scanner(char s[]);
bool isVowel(char);
int length(char s[]);

int main(){

//string sentence;
const int max=4096;
char sentence[max];
cout << "Enter $ to terminate!" <<endl;
do{
cout << "\nEnter the input string: ";
getline(sentence,max);
scanner(sentence);
}while(sentence != "$");
return 0;
}

void scanner(char s[]){
int stringSize = length(char s[]);
char current,next;
int count = 0;
int aa,ae,ai,ao,au,ea,ee,ei,eo,eu,ia,ie,ii,io,iu,oa,oe,oi,oo,ou,
ua,ue,ui,uo,uu;
aa = ae = ai = ao = au = 0;
ea = ee = ei = eo = eu = 0;
ia = ie = ii = io = iu = 0;
oa = oe = oi = oo = ou = 0;
ua = ue = ui = uo = uu = 0;
while(count < stringSize){
current = s[count];
if( (count + 1) == stringSize)
break;
next = s[count + 1];
if(isVowel(current) && isVowel(next)){
//a
current = tolower(current);
next = tolower(next);
if( current == 'a' && next == 'a')
aa++;
else if( current == 'a' && next == 'e')
ae++;
else if( current == 'a' && next == 'i')
ai++;
else if( current == 'a' && next == 'o')
ao++;
else if( current == 'a' && next == 'u')
au++;
//e
else if( current == 'e' && next == 'a')
ea++;
else if( current == 'e' && next == 'e')
ee++;
else if( current == 'e' && next == 'i')
ei++;
else if( current == 'e' && next == 'o')
eo++;
else if( current == 'e' && next == 'u')
eu++;
//i
else if( current == 'i' && next == 'a')
ia++;
else if( current == 'i' && next == 'e')
ie++;
else if( current == 'i' && next == 'i')
ii++;
else if( current == 'i' && next == 'o')
io++;
else if( current == 'i' && next == 'u')
iu++;
//o
else if( current == 'o' && next == 'a')
oa++;
else if( current == 'o' && next == 'e')
oe++;
else if( current == 'o' && next == 'i')
oi++;
else if( current == 'o' && next == 'o')
oo++;
else if( current == 'o' && next == 'u')
ou++;
//u
else if( current == 'u' && next == 'a')
ua++;
else if( current == 'u' && next == 'e')
ue++;
else if( current == 'u' && next == 'i')
ui++;
else if( current == 'u' && next == 'o')
uo++;
else /*( current == 'u' && next == 'u')*/
uu++;

count++;
}
else{
count++;
continue;
}
}
int totalVowels = aa + ae + ai + ao + au +
ea + ee + ei + eo + eu +
ia + ie + ii + io + iu +
oa + oe + oi + oo + ou +
ua + ue + ui + uo + uu ;
if(totalVowels == 0){
cout << "No double vowels!" <<endl;
}
if(aa > 0){
cout << aa <<"x "<<"aa";
totalVowels -= aa;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ae > 0){
cout << ae <<"x "<<"ae";
totalVowels -= ae;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ai > 0){
cout << ai <<"x "<<"ai";
totalVowels -= ai;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ao > 0){
cout << ao <<"x "<<"ao";
totalVowels -= ao;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(au > 0){
cout << au <<"x "<<"au";
totalVowels -= au;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ea > 0){
cout << ea <<"x "<<"ea";
totalVowels -= ea;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ee > 0){
cout << ee <<"x "<<"ee";
totalVowels -= ee;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ei > 0){
cout << ei <<"x "<<"ei";
totalVowels -= ei;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(eo > 0){
cout << eo <<"x "<<"eo";
totalVowels -= eo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(eu > 0){
cout << eu <<"x "<<"eu";
totalVowels -= eu;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ia > 0){
cout << ia <<"x "<<"ia";
totalVowels -= ia;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ie > 0){
cout << ie <<"x "<<"ie";
totalVowels -= ie;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ii > 0){
cout << ii <<"x "<<"ii";
totalVowels -= ii;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(io > 0){
cout << io <<"x "<<"io";
totalVowels -= io;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(iu > 0){
cout << iu <<"x "<<"iu";
totalVowels -= iu;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oa > 0){
cout << oa <<"x "<<"oa";
totalVowels -= oa;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oe > 0){
cout << oe <<"x "<<"oe";
totalVowels -= oe;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oi > 0){
cout << oi <<"x "<<"oi";
totalVowels -= oi;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oo > 0){
cout << oo <<"x "<<"oo";
totalVowels -= oo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ou > 0){
cout << ou <<"x "<<"ou";
totalVowels -= ou;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ua > 0){
cout << ua <<"x "<<"ua";
totalVowels -= ua;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ue > 0){
cout << ue <<"x "<<"ue";
totalVowels -= ue;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ui > 0){
cout << ui <<"x "<<"ui";
totalVowels -= ui;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(uo > 0){
cout << uo <<"x "<<"uo";
totalVowels -= uo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(uu > 0){
cout << uu <<"x "<<"uu"<<".";
totalVowels -=uu;
}
}

bool isVowel(char c){
return (c == 'a' || c == 'A' || c == 'e' ||
c == 'E' || c == 'i' || c == 'I' || c == 'o' ||
c == 'O' || c == 'u' || c == 'U') ;
}

int length(char s[]){

int i=0;
while(s='\0')
i=i+1;

return i;



}
 
K

Kai-Uwe Bux

Prof.Stanley said:
HELLO ALL I AM TRYING TO PUT UP A PROGRAM THAT DETECTS DOUBLE VOWELS
AFTER RECIEVING AN INPUT STRING; MY MAIN PROBLEM LIES IN THE INPUT CAN
SOMEBODY KINDLY HELP DETECT ANY SYNTAX OR SEMANTIC ERRORS:

a) Your keyboard has a problem with CAPS-LOCK. You should fix that.

b) For best diagnostics, try Comeau online.

Suggestions and criticism that a human can provide follow below.
HERE IS THE CODE:



#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void scanner(char s[]);
bool isVowel(char);
int length(char s[]);

Note:

a) if the string is 0-terminated, then you should use std::strlen().
b) if the string is not 0-terminated, you are out of luck.
c) you should use strings anyway.
int main(){

//string sentence;
const int max=4096;
char sentence[max];

Would you use strings, there would be no such arbitrary limitation.
cout << "Enter $ to terminate!" <<endl;
do{
cout << "\nEnter the input string: ";
getline(sentence,max);

std::cin.getline( sentence, max );
scanner(sentence);
}while(sentence != "$");
return 0;
}

Since you used getline and max, the program will not crash nor produce
undefined behavior. It will just not count correctly if there are lines
longer than 4096 characters.

It will also not count correctly, if someone prepares a file that contains 0
characters in some lines.
void scanner(char s[]){
int stringSize = length(char s[]);
char current,next;
int count = 0;
int aa,ae,ai,ao,au,ea,ee,ei,eo,eu,ia,ie,ii,io,iu,oa,oe,oi,oo,ou,
ua,ue,ui,uo,uu;
aa = ae = ai = ao = au = 0;
ea = ee = ei = eo = eu = 0;
ia = ie = ii = io = iu = 0;
oa = oe = oi = oo = ou = 0;
ua = ue = ui = uo = uu = 0;
while(count < stringSize){
current = s[count];
if( (count + 1) == stringSize)
break;
next = s[count + 1];
if(isVowel(current) && isVowel(next)){
//a
current = tolower(current);
next = tolower(next);
if( current == 'a' && next == 'a')
aa++;
else if( current == 'a' && next == 'e')
ae++;
else if( current == 'a' && next == 'i')
ai++;
else if( current == 'a' && next == 'o')
ao++;
else if( current == 'a' && next == 'u')
au++;
//e
else if( current == 'e' && next == 'a')
ea++;
else if( current == 'e' && next == 'e')
ee++;
else if( current == 'e' && next == 'i')
ei++;
else if( current == 'e' && next == 'o')
eo++;
else if( current == 'e' && next == 'u')
eu++;
//i
else if( current == 'i' && next == 'a')
ia++;
else if( current == 'i' && next == 'e')
ie++;
else if( current == 'i' && next == 'i')
ii++;
else if( current == 'i' && next == 'o')
io++;
else if( current == 'i' && next == 'u')
iu++;
//o
else if( current == 'o' && next == 'a')
oa++;
else if( current == 'o' && next == 'e')
oe++;
else if( current == 'o' && next == 'i')
oi++;
else if( current == 'o' && next == 'o')
oo++;
else if( current == 'o' && next == 'u')
ou++;
//u
else if( current == 'u' && next == 'a')
ua++;
else if( current == 'u' && next == 'e')
ue++;
else if( current == 'u' && next == 'i')
ui++;
else if( current == 'u' && next == 'o')
uo++;
else /*( current == 'u' && next == 'u')*/
uu++;

count++;
}
else{
count++;
continue;
}
}
int totalVowels = aa + ae + ai + ao + au +
ea + ee + ei + eo + eu +
ia + ie + ii + io + iu +
oa + oe + oi + oo + ou +
ua + ue + ui + uo + uu ;
if(totalVowels == 0){
cout << "No double vowels!" <<endl;
}
if(aa > 0){
cout << aa <<"x "<<"aa";
totalVowels -= aa;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ae > 0){
cout << ae <<"x "<<"ae";
totalVowels -= ae;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ai > 0){
cout << ai <<"x "<<"ai";
totalVowels -= ai;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ao > 0){
cout << ao <<"x "<<"ao";
totalVowels -= ao;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(au > 0){
cout << au <<"x "<<"au";
totalVowels -= au;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ea > 0){
cout << ea <<"x "<<"ea";
totalVowels -= ea;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ee > 0){
cout << ee <<"x "<<"ee";
totalVowels -= ee;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ei > 0){
cout << ei <<"x "<<"ei";
totalVowels -= ei;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(eo > 0){
cout << eo <<"x "<<"eo";
totalVowels -= eo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(eu > 0){
cout << eu <<"x "<<"eu";
totalVowels -= eu;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ia > 0){
cout << ia <<"x "<<"ia";
totalVowels -= ia;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ie > 0){
cout << ie <<"x "<<"ie";
totalVowels -= ie;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ii > 0){
cout << ii <<"x "<<"ii";
totalVowels -= ii;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(io > 0){
cout << io <<"x "<<"io";
totalVowels -= io;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(iu > 0){
cout << iu <<"x "<<"iu";
totalVowels -= iu;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oa > 0){
cout << oa <<"x "<<"oa";
totalVowels -= oa;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oe > 0){
cout << oe <<"x "<<"oe";
totalVowels -= oe;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oi > 0){
cout << oi <<"x "<<"oi";
totalVowels -= oi;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(oo > 0){
cout << oo <<"x "<<"oo";
totalVowels -= oo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ou > 0){
cout << ou <<"x "<<"ou";
totalVowels -= ou;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ua > 0){
cout << ua <<"x "<<"ua";
totalVowels -= ua;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ue > 0){
cout << ue <<"x "<<"ue";
totalVowels -= ue;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(ui > 0){
cout << ui <<"x "<<"ui";
totalVowels -= ui;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(uo > 0){
cout << uo <<"x "<<"uo";
totalVowels -= uo;
if(totalVowels == 0)
cout <<".";
else cout <<", ";
}
if(uu > 0){
cout << uu <<"x "<<"uu"<<".";
totalVowels -=uu;
}
}

This function mixes analysis and output. Not good. Moreover, it is a
maintainance nightmare. You should consider using something like this:

typedef std::pair< char, char > CharacterPair;
std::map< CharacterPair, unsigned long > PairCount;

void record_vowel_pair( char first, char second, PairCount & count ) {
if ( isVowel(first) && isVowel(second) ) {
++ count[ CharacterPair(first,second) ];
}
}

Maybe some call of tolower() is in order somewhere; that depends on what you
want.
bool isVowel(char c){
return (c == 'a' || c == 'A' || c == 'e' ||
c == 'E' || c == 'i' || c == 'I' || c == 'o' ||
c == 'O' || c == 'u' || c == 'U') ;
}

int length(char s[]){

int i=0;
while(s='\0')
i=i+1;

return i;



}




Finally, you should reconsider the design: what about something that works
on iterators:

template < typename CharInputIterator >
PairCount record_vowel_pairs ( CharInputIterator from,
CharInputIterator to );

With that signature, main could be like so:

int main ( void ) {
PairCount count =
record_vowel_pairs( std::istream_iterator< char >( std::cin ),
std::istream_iterator< char >() );
print_some_statistics( count );
}

Or, if you insist on doing stuff line by line:

int main ( void ) {
std::string line;
while ( std::cin >> line ) {
PairCount count = record_vowel_pairs( line.begin(), line.end() ) );
print_some_statistics( count );
}
}


Warning: none of my code has been checked by a compiler. It will contain
typos.



Best

Kai-Uwe Bux
 
B

benben

Prof.Stanley said:
HELLO ALL I AM TRYING TO PUT UP A PROGRAM THAT DETECTS DOUBLE VOWELS
AFTER RECIEVING AN INPUT STRING; MY MAIN PROBLEM LIES IN THE INPUT CAN
SOMEBODY KINDLY HELP DETECT ANY SYNTAX OR SEMANTIC ERRORS:

HERE IS THE CODE:



#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void scanner(char s[]);

void scanner(const char s[]);
bool isVowel(char);
int length(char s[]);

int length(const char s[]);

Const correctness matters.
int main(){

//string sentence;
const int max=4096;
char sentence[max];
cout << "Enter $ to terminate!" <<endl;
do{
cout << "\nEnter the input string: ";
getline(sentence,max);
scanner(sentence);
}while(sentence != "$");

First off, you can't compare two raw strings with the != operator. You
should use std::strcmp:

strcmp(sentence, "$")

or, since you are only interested in the first character:

sentence[0] != '$'

Secondly, you should always check the stream state in a loop. Usually
I'd go with:

while (cin)
{
cout << "blah";
getline(sentence, max);

if (sentence[0] != '$')
scanner(sentence);
}

At least you get the idea.
return 0;
}

void scanner(char s[]){

[260 lines of code snipped]

Wow! That function is way longer than it really should be. But before
giving any tips and hints I'm going to hit you with a number of compiler
error and logical errors.

One way to simplify design is to use the standard library facilities.
For example, a simple scanner can be trivially written as:

void scanner(const char s[])
{
vector<char> vstack;
map<string, unsigned int> m;
unsigned int l = strlen(s);

for (unsigned int i = 0; i <= l; ++i)
{
char c = s;

if (isVowel(c))
{
vstack.push_back(tolower(c, locale()));
}
else
{
if (buff.size() == 2)
{
vstack.push_back(0);
++m[&vstack[0]];
}

vstack.clear();
}
}

// print out the content of m, left for exercise
}


bool isVowel(char c){
return (c == 'a' || c == 'A' || c == 'e' ||
c == 'E' || c == 'i' || c == 'I' || c == 'o' ||
c == 'O' || c == 'u' || c == 'U') ;
}

This function isn't lengthy but it is made very inflexible. Consider:

static char vowels[] = "aeiouAEIOU";

for (unsigned int i = 0; i < 10; ++i)
if (c == vowels) return true;

return fail;

int length(char s[]){

int i=0;
while(s='\0')
i=i+1;

return i;



}


That's just strlen...why do you reinvent the wheel?
 
O

osmium

:

if( current == 'a' && next == 'a')
aa++;
else if( current == 'a' && next == 'e')
ae++;
else if( current == 'a' && next == 'i')
ai++;
else if( current == 'a' && next == 'o')
ao++;
else if( current == 'a' && next == 'u')
au++;
//e
else if( current == 'e' && next == 'a')
ea++;
else if( current == 'e' && next == 'e')
ee++;
else if( current == 'e' && next == 'i')
ei++;
else if( current == 'e' && next == 'o')
eo++;
else if( current == 'e' && next == 'u')
eu++;
//i
else if( current == 'i' && next == 'a')
ia++;
else if( current == 'i' && next == 'e')
ie++;
else if( current == 'i' && next == 'i')
ii++;
else if( current == 'i' && next == 'o')
io++;
else if( current == 'i' && next == 'u')
iu++;
//o
else if( current == 'o' && next == 'a')
oa++;
else if( current == 'o' && next == 'e')
oe++;
else if( current == 'o' && next == 'i')
oi++;
else if( current == 'o' && next == 'o')
oo++;
else if( current == 'o' && next == 'u')
ou++;
//u
else if( current == 'u' && next == 'a')
ua++;
else if( current == 'u' && next == 'e')
ue++;
else if( current == 'u' && next == 'i')
ui++;
else if( current == 'u' && next == 'o')
uo++;
else /*( current == 'u' && next == 'u')*/
uu++;
<snip>

Kind of on the wordy end of the spectrum. Take a look at this and see if
you can extract some ideas you can use.

----------------------------
#include <iostream>
#include <cctype> // tolower()
using namespace std;

const int a=1, e=2, i=3, o=4, u=5;

// return 0 if not a vowel
int classify(char c)
{
c = tolower(c);
switch(c)
{
case 'a' : return a;
case 'e' : return e;
case 'i' : return i;
case 'o' : return o;
case 'u' : return u;
default : return 0;
}
}
//=================
int main()
{
// static variables will be initialized to zero by the system
static int tally[6][6]; // allow for zero [first vowel][last vowel]
char sentence[] = "moot moon mean affair moon moUse";
int beg = 0, end; // first vowel, last vowel. Avoid l looks like 1
problem
for(int i=0; sentence; i++)
{
end = classify(sentence);
if(beg)
tally[beg][end]++;
beg = end;
}
// done. show output
for(int i=1; i<6; i++)
{
for(int j=1; j<6; j++)
cout << tally[j];
cout << endl;
}
cin.get();
cin.get();
}
 
T

Tomás

Prof.Stanley posted:
HELLO ALL I AM TRYING TO PUT UP A PROGRAM THAT DETECTS DOUBLE VOWELS
AFTER RECIEVING AN INPUT STRING; MY MAIN PROBLEM LIES IN THE INPUT CAN
SOMEBODY KINDLY HELP DETECT ANY SYNTAX OR SEMANTIC ERRORS:


I already told you how disgusting and retarded your code is, and I even
took the time to write you much nicer code. How about you read the replies
to your posts before you just go and post some more.


-Tomás
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Prof.Stanley said:
HELLO ALL I AM TRYING TO PUT UP A PROGRAM THAT DETECTS DOUBLE VOWELS
AFTER RECIEVING AN INPUT STRING; MY MAIN PROBLEM LIES IN THE INPUT CAN
SOMEBODY KINDLY HELP DETECT ANY SYNTAX OR SEMANTIC ERRORS:

HERE IS THE CODE:

I CANNOT HEAR YOU.... THERE'S SO MUCH NOISE IN HERE, THAT I MUST TURN ON
THE CAPS LOCK KEY, JUST SO OTHERS CAN HEAR ME SHOUTING.


Best regards / Med venlig hilsen
Martin Jørgensen
 
B

benben

OP's problem is not just his style but his algorithm. Scanning for
double-vowels are simple enough to be squeezed in a 30- lines function,
even if regular expression is not used.

A simple solution is to use a stack and a map. Push vowels to the stack
until a consonant is found, then pop the stack and add the record to a
map counter.

Regards,
Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top