Thread: Need big help with a menu program

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Need big help with a menu program

    I have a problem in "c" programing. I have to make a program organized as a menu, a program which reads text files, records the results in a file ( the user has to enter the names of both files) and has these capabilities:
    - Determines the longest word, which consists of letters only;
    - Determines the common letters in each line;
    - Can change one digit with another;
    - Determines the longest and shortest line in the file.
    Can you please help me? At least for one of the following points....
    Thx in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried? At least for one of the above-mentioned points....
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    i tried the one with the longest word coz i cant write the others
    Code:
     void longest_word(FILE *f,FILE *f1)
       {    
              char data[2000];
                int i,len=0,len1=0,k=0;
         for(i=0;i<2000;i++)
         {   
          if(data[i]==EOF) break;
          data[i]=fgetc(f);
          if((data[i]>='a'&&data[i]<='z')||(data[i]>='A'&&data[i]<='Z'))
           {   len++;}
           else if(len>len1)
           {len1=len; k = i-len; len=0;}
          }
        printf("%d",s1);
        for(i=k;i<k+len1;i++)
         printf("%c",data[i2]);
         i++;  
       }
    dont know if its good thou

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Either your function for finding the longest word in a line of text works, or it doesn't. You surely didn't write the code, and then never test it.

    For finding the most common letter, why not find it the same way that you'd find it by hand, with a paper and penci. For example, using the word "BALL":
    Code:
    ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
    ========================================================
    11         1
               1 
    ------------------
    A: 1
    B: 1
    L: 2
    You don't have to have an if statement for every letter, just use a char (or int) array, and increment the value of the array element that corresponds with the letter:
    Code:
    for(i=0; i <=strlen(your string of char's; i++) 
      your_char_array[i-'0']++;  //i minus zero
    If you work out the steps you would take to do this by hand, you can use those steps as the foundation for coding up the function to do this, in C

    You can't teach the computer to do something you don't know the broad steps to do, yourself.
    Last edited by Adak; 12-17-2010 at 08:26 PM.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    oki this is what i got but i cant make all of them run as they should
    Code:
    #include"stdafx.h"
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    #include<string.h> 
    #include<stdlib.h>
    #include<ctype.h>
    
        int choice(void);   // izbor na jelanata ot potrebitelq operaciq 
    	void longest_word(FILE *f,FILE *f1);
    	void change_word(FILE *f,FILE *f1);	
    	void longest_row(FILE *f,FILE *f1); //	Определяне броят на думите на всеки ред 
    	
    
        int main()		// glavnata fynkciq 
    
        {  
    	   char mass[40],mass1[40];
    	   FILE *p,*p1;
    	   int choicee;
    
    		printf("Enter file name that you want to write\n");
    		gets(mass1);
    		 if((p=fopen("C:\\Users\\Tzvetelin\\Desktop\\Ceco\\Ceco.txt","rt"))== NULL){
    			printf("Error while reading:");
    			getch();
    			return 1;
    		}
    		if(!(p1=fopen(mass1,"wt"))){
    			printf("Error while reading %s",mass1);
    			getch();
    		   return 2 ; 
    		}
    
    	   do {
    		choicee=choice();
     
    	   switch(choicee){
    		   case 1:longest_word(p,p1);break;
    			   case 2: change_word (p,p1);break;
    				   case 3: longest_row(p,p1);break;
    		   default: printf("You enter wrong number,please try again... \n");
    	       }
    	   }
    	     while(choicee<1||choicee>4);
    	      getch();
    	      return 0;
    		}
    
    
            int choice(void) 
    	  {    int a;
    		  printf("1Determination of the longest word consisting only of letters\n");
    		  printf("2.Change number\n");
    		  printf("3.Longest row\n");
    		  printf("4.Determination of the longest and the shortest line in the file\n");
    		  printf("Enter number of your selection\n");
    		  scanf("%d",&a);
    		  return a;
    	  }
    
    
          void longest_word(FILE *f,FILE *f1)
       {    
              char data[2000];
                int i,len=0,len1=0,k=0;
         for(i=0;i<2000;i++)
         {   
          if(data[i]==EOF) break;
          data[i]=fgetc(f);
          if((data[i]>='a'&&data[i]<='z')||(data[i]>='A'&&data[i]<='Z'))
           {   len++;}
           else if(len>len1)
           {len1=len; k = i-len; len=0;}
          }
      
        for(i=k;i<k+len1;i++)
         fprintf(f1,"%c",data[i]);
    	     i++;  
       }
    
    
    	   void change_word(FILE *f,FILE *f1)
    	   {  int a1,a,tr;
                  
                  for(;;)
    		   {  	if(fscanf(f,"%d",&a1)==EOF)break;
    			     
    			  if(a1 >=32 && a1<=47 || a1>=58&& a1<=127)tr=0;
    			    else
    				{
    					printf("Vuvedete 4islo:\n");
    					scanf("%d",&a);
    			        a1=a;
    			    }
                  fprintf(f1,"%d\t",a1);
    			  }
    			  rewind(f);
    	   }
    
    	   void longest_row(FILE *f,FILE *f1)
    	    {  int k=0,br=0,br1=0,k1=0,a1; 
    	        int j=0,j1=0,b1;
    
    			for(;;)
    			 {  if((a1=fgetc(f))==EOF)break;
    				 br++;
    				 if(a1=='\n')
    				   {  k++;
    					 if(br>br1)
    				 { br1=br;k1=k;br=0;}
    			      }
    			}
    
    			fprintf(f1,"nai dulgiq red e %d \n",k1);
    
                  rewind(f);
    
    
                   br=0;
    			for(;;)
    			{  if((b1=fgetc(f))==EOF)break;
    				
    					br1=1000;
    				 br++;
    				 if(b1=='\n')
    				   {  j++;
    					 if(br<br1)
    				 { br1=br;j1=j;br=0;}
    				 }
    			}
    
    			fprintf(f1,"nai kusiq red e %d\n",j1);
    	   }

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    can you help now ? pls

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Dear MOG, such indentation, I have never seen!

    The FIRST thing you need to do is learn to indent your code. The subordinate lines of code should be indented according to their relative sub-ordinance, in your programs logic.

    No one is going to want to study code like this.

    Reformat it and repost it, and learn to indent (I can barely restrain myself from using inappropriate language here). It's that bad.
    Last edited by Adak; 12-18-2010 at 05:09 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by angelofmyst View Post
    can you help now ? pls
    You need to learn how to ask questions the smart way. Or, you know, to actually ask a question. Like the above poster said, no one wants to read all of that and then just guess/sort out what it is you're actually having problems with.

    You don't just walk to the mechanic, show him a picture of your car, and tell him to tell you what is wrong with it. That would be dumb. That is what you are doing here. If you have a specific problem with what you're trying to do, how about actually saying what the problem is?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on Classic big integer program
    By DaNxTh3xMaNx in forum C Programming
    Replies: 16
    Last Post: 09-14-2010, 10:11 PM
  2. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  3. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. C menu driven program
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 08:56 AM