SYNTAX AND SEMANTICS

P

Prof.Stanley

HELLO ALL I AM NOT VERY OLD IN C AND C++ SO I AM TRYING TO WRITE A
PROGRAM THAT RECIEVES AN INPUT STRING AND DETECTS DOUBLE VOWELS IN
IT:pLEASE WILL SOEMBODY OF GOOD FAITH HELP TO CORRECT ANY SYNTAX OR
SEMENTIC ERRORS WHICH WILL HELP DISCARD THE FIVE ERRORS I AM LEFT WITH:


#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;



}
 
M

Marc Thrun

Prof.Stanley said:
HELLO ALL I AM NOT VERY OLD IN C AND C++ SO I AM TRYING TO WRITE A
PROGRAM THAT RECIEVES AN INPUT STRING AND DETECTS DOUBLE VOWELS IN
IT:pLEASE WILL SOEMBODY OF GOOD FAITH HELP TO CORRECT ANY SYNTAX OR
SEMENTIC ERRORS WHICH WILL HELP DISCARD THE FIVE ERRORS I AM LEFT WITH:
Please don't shout, it won't help you to get any useful answer.
#include <iostream>
[snip]

This is not C but C++, you might get better advice in comp.lang.c++.
 
F

Frank Silvermann

Marc said:
Prof.Stanley said:
HELLO ALL I AM NOT VERY OLD IN C AND C++ SO I AM TRYING TO WRITE A
PROGRAM THAT RECIEVES AN INPUT STRING AND DETECTS DOUBLE VOWELS IN
IT:pLEASE WILL SOEMBODY OF GOOD FAITH HELP TO CORRECT ANY SYNTAX OR
SEMENTIC ERRORS WHICH WILL HELP DISCARD THE FIVE ERRORS I AM LEFT WITH:
Please don't shout, it won't help you to get any useful answer.
#include <iostream>
[snip]

This is not C but C++, you might get better advice in comp.lang.c++.
I think your predicament with io highlights the advantages and drawbacks
of the two languages. It's nice to be able to write cout<<'print the
darn thing' and see something on the monitor. It avoids all the
headaches attending a printf() with strong and enforced types. But when
you endeavor to remove those last five errors or desire to port, the
effort spent during those headaches bears dividends. frank
 
M

Martin Ambuhl

Prof.Stanley said:
HELLO ALL I AM NOT VERY OLD IN C AND C++ [etc.]

Typing in all caps is considered shouting and is often considered rude.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

None of those three headers is a standard C header, and the fourth line
is a syntax error in C. I suppose you are using that obscure and
overburdened language C++, which has its own newsgroup
< If my supposition is correct, then post to that
newsgroup, but not before checking their FAQ and following the newsgroup.

Note: the archives at Google Groups will allow you to "follow"
newsgroups much faster than real time. Since most newsgroups post
partial FAQs or links to their FAQs about twice a month, a little over
two weeks would be a natural minimum for following a newgroup.
 
J

Joe Estock

Prof.Stanley said:
HELLO ALL I AM NOT VERY OLD IN C AND C++ SO I AM TRYING TO WRITE A
PROGRAM THAT RECIEVES AN INPUT STRING AND DETECTS DOUBLE VOWELS IN
IT:pLEASE WILL SOEMBODY OF GOOD FAITH HELP TO CORRECT ANY SYNTAX OR
SEMENTIC ERRORS WHICH WILL HELP DISCARD THE FIVE ERRORS I AM LEFT WITH:


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

#include <stdio.h>
#include <string.h>
#include <ctype.h>

Like others following this thread have already pointed out, none of the
above is related to the C language. You want C++ which is down the hall
on the right. I will pretend that the above has been corrected as
suggested for the remainder of my reply.
void scanner(char s[]);
bool isVowel(char);

C++ might have a type called 'bool' by default, but in C we need
stdbool.h or you need to create your own.
int length(char s[]);

int main(){

This should be either:

int main(int argc, char **argv)

OR

int main(void)
//string sentence;
const int max=4096;
char sentence[max];
cout << "Enter $ to terminate!" <<endl;

printf("%s", "Enter $ to terminate!\n");
do{
cout << "\nEnter the input string: ";
getline(sentence,max);

What is 'getline'? The standard knows nothing about this function.
scanner(sentence);
}while(sentence != "$");

This is illegal in C and will produce a diagnostic. You want either
strcmp() or while(sentence[0] != '$') however this will fail if there is
leading space before it and it does not check for anything after the
first element of the array.
return 0;
}

void scanner(char s[]){
int stringSize = length(char s[]);

I have no idea what 'length' is either since it is not part of the
standard. Perhaps you meant strlen() which can be found in string.h
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;

Syntactically this is correct, however it seems insane to do something
using this many variables. An array would be easier to maintain and you
could always define a constant to refer to each individual element of
the array. For example:

#define AA 0
#define AE 1
#define AI 2

etc...

int myArray[25];
int i = 0;

for (i = 0; i < 25; i++)
{
myArray = 0;
}
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){

I would use a for loop here, however this is more of a matter of style I
guess.
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++;

This is hideous. At the very least use a switch instead of multiple
if..else if's
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 ;

This is illegal in C. Declarations must begin at the top of the coding
block (ideally immediately after the initial { after the function
declaration).
if(totalVowels == 0){
cout << "No double vowels!" <<endl;
}
if(aa > 0){
cout << aa <<"x "<<"aa";

Again, printf("%s x aa\n", aa);
Or if you do not want the newline yet omit it and do fflush(stdout);

[snip remainder of code]

I stopped looking at your code after this point. Fix the above issues
and attempt to modify the remaining code using the same guidelines as
above and if you are still having trouble after that feel free to post
your code and issues again.

Joe
 
K

Keith Thompson

Joe Estock said:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

Like others following this thread have already pointed out, none of
the above is related to the C language. You want C++ which is down the
hall on the right. I will pretend that the above has been corrected as
suggested for the remainder of my reply.

I'm not sure I see the point of this. The OP simply posted to the
wrong newsgroup. Pretending that the code is *supposed* to be C, and
then citing each individual case where the program uses some C++
feature that doesn't exist in C, seems like a waste of time.

If the OP wants help with this program, he can either re-write it in C
and post it here, or post it in comp.lang.c++. The latter is a lot
easier.

There are certainly some serious problems with the program other than
the fact that it's not C, but I'm sure the folks in comp.lang.c++ are
capable of dealing with them.

[...]
This should be either:

int main(int argc, char **argv)

OR

int main(void)

Yes, it should, but int main(){...} is still legal.

[...]
This is illegal in C. Declarations must begin at the top of the coding
block (ideally immediately after the initial { after the function
declaration).

Mixing declarations and statements is legal in C99.

[...]
 
D

Dave Thompson

Prof.Stanley wrote:
int length(char s[]);
<snip other points I agree with
or already corrected by Keith Thompson (no relation)>
int main(void)
//string sentence;
const int max=4096;
char sentence[max];

This is only valid in C99, or C++. C89 requires a constant expression
for array dimension, and const int does not qualify.
printf("%s", "Enter $ to terminate!\n");


What is 'getline'? The standard knows nothing about this function.
That is another C++ism.
scanner(sentence);
}while(sentence != "$");

This is illegal in C and will produce a diagnostic. You want either
strcmp() or while(sentence[0] != '$') however this will fail if there is
leading space before it and it does not check for anything after the
first element of the array.
It is not illegal and no diagnostic is required. It compares the
decayed pointer to sentence to a decayed pointer to the string
literal, which is totally useless and a good compiler might warn.
return 0;
}

void scanner(char s[]){
int stringSize = length(char s[]);

I have no idea what 'length' is either since it is not part of the
standard. Perhaps you meant strlen() which can be found in string.h
He probably meant the length() that was declared at the top of his
post and defined at the bottom. Although strlen() would be fine.

Nit: the declaration is found in string.h. Not the function itself.
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;

Syntactically this is correct, however it seems insane to do something
using this many variables. An array would be easier to maintain and you
could always define a constant to refer to each individual element of
the array. For example:

#define AA 0
#define AE 1
#define AI 2

etc...

int myArray[25];
int i = 0;

for (i = 0; i < 25; i++)
{
myArray = 0;
}

Or even better [5][5] and process (encode) 'current' and 'next'
separately. And can initialize with just = {0} or for my alternate 2D
optionally instead = {{0}} which avoids a silly warning in gcc.
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){

I would use a for loop here, however this is more of a matter of style I
guess.
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 you do these first, isVowel only needs to deal with lowercase. And
it could do the encoding for an array by returning 0..4 for aeiou and
-1 for nonvowel. Then you only need
if( code1 >= 0 && code2 >= 0 ) ++count[code1][code2];

Or if you prefer return 1..5 (which C considers 'true') or 0, and
if( code1 && code2 ) ++count[code1-1][code2-1];

<snip lots>

- David.Thompson1 at worldnet.att.net
 

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

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top