Jump to content

How to fix Errors in this program to achieve satisfaction

- - - - -

  • Please log in to reply
5 replies to this topic

#1
renren00

renren00

    Newbie

  • Members
  • PipPip
  • 10 posts
#include <stdio.h>
#include <conio.h>
int main()
{
char str[80];
int cnt[122];
int i;
for (i=0;i<122;++i)
cnt[i]=0;
clrscr();
printf("Enter a string less than 80 characters: ");
gets( str );
int wrong = 0;
for(i=0; str[i] != '\0'; ++i)
{
if (((str[i]>='a') && (str[i]<='z'))||((str[i]>='A') && (str[i]<='Z')))
{
++cnt[str[i]];
}
else
{
printf("Wrong Input.\n");
return 1;
}
}
for (i=0;i<122;++i)
{
if (cnt[i]>0)
{
printf ("%c = %d\n",(char)i,cnt[i]);
}
}

getch();
return 0;
}


1. when i run the program it says "Expression syntax error on function main." in the expression int wrong = 0;

2. and when i enter a numerical character. it doesn't say Wrong input, although we put Wrong input in else, but instead it closes the program and turns back to the blue IDE. It should say wrong input and will require a user to enter another word.

3. and when i enter a compound word like mother-in-law , it will display the character '-' and will show how many times it appear.
Though it should not be, only the alphabet would be display e.g. (mother-in-law = a,e,h,i,l,m,n,r,t,w) the dash must not be displayed.

Edited by WingedPanther, 28 January 2012 - 06:24 PM.
add code tags (the # button)


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
1. when you run it or attempt to compile it?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
renren00

renren00

    Newbie

  • Members
  • PipPip
  • 10 posts
yes when i try to run it

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
1. I can't duplicate the error. What compiler are you using, I used GCC 4.6.1.

2. I entered "12345" and got "Wrong Input." for output

3. I entered "mother-in-law" and got "Wrong Input." for output.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
renren00

renren00

    Newbie

  • Members
  • PipPip
  • 10 posts
we are using the old turbo c from the school which i believe is the old one. mother-in-law should be allowed because dash is the only special character to be allowed but it must not display in the analysis part in which the program will count it too.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Is there a reason you're using Turbo C? That will cause all kinds of problems if you're trying to learn modern C.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users