string problem

This is a discussion on string problem within the C Programming forums, part of the General Programming Boards category; I'm still trying to figure out how to loop through a string so that every time I loop through it ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    9

    string problem

    I'm still trying to figure out how to loop through a string so that every time I loop through it the list will be updated with the most recent values at the top.

    This is what I have, but it is not working:

    Code:
    //Declarations
    char startunit[20][5];
    	char endunit[20][5];
    	char filename[20][15];
    
    //string assignments
    	strcpy(startunit[i],"mm");
    	strcpy(endunit[i],"mm");
    	strcpy(filename[i],"mm.txt");
    
    //display
           printf("%s", startunit[i]);
    	printf("%s", endunit[i]);
    	printf("%s", filename[i]);
    All I get is some garbage that displays.

    I only have 10.5 hrs till this is due. I really need some help on this. Once I figure this problem out it will be easy for me to complete my project.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I see nothing wrong with anything you've posted. Some things to check would be:
    (1) Make sure i runs from 0 to 19 (using a for-loop for (i=0; i<20; i++))
    (2) Make sure startunit and endunit are no more than 4 characters.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    I could not get it to work.
    I am going to post my entire code so I might be able to get some help with this. I t keeps displaying garbage. I only have part of it posted, and the declarations are only used in one case if the switch statement so I can test it. Sorry about the length.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[])
    {
    	char check[5];
    	int i=0;
    	int j=1;
    	int start;
        int start1;
        int end;
    	double mm;
    	double ans;
    	FILE *mmptr;
    	char startunit[20][5];
    	char endunit[20][5];
    	char filename[20][15];
    	
        printf("Welcome to Casey's Conversion Program\n");
        
        
        startunit[i] == startunit[i+1];
        endunit[i] == endunit[i+1];
        filename[i] == filename[i+1];
    	
        /* loop to rerun program */
    	
        do{
                
    	printf("Enter:\n1 for Length Conversion\n");
    	printf("2 for Area Conversion\n");
    	printf("3 for Volume Conversion\n");
    	printf("4 for Time Conversion\n");
    	printf("5 for Angle Conversion\n");
    	printf("6 for Speed Conversion\n");
    	printf("7 for Acceleration Conversion\n");
    	printf("8 for Mass Conversion\n");
    	printf("9 for Force Conversion\n");
    	printf("10 for Pressure Conversion\n");
    	printf("11 for Energy Conversion\n");
    	printf("12 for Power Conversion\n");
    	scanf("%d", &start);
    
    /*Length Conversion*/
    
    	switch(start){
    		
            case 1:
        
        printf("Enter starting unit: \n");
    	printf("1 for millimeters\n");
    	printf("2 for centimeters\n");
    	printf("3 for meters\n");
    	printf("4 for kilometers\n");
    	printf("5 for inches\n");
    	printf("6 for feet\n");
    	printf("7 for yards\n");
    	printf("8 for miles\n");
    	scanf("%d", &start1);
    
    	switch(start1){
     
             case 1:
    			
       mmptr = fopen("mm.txt", "a");
    	
    	printf("Enter the number of millimeters: ");
    	scanf("%lf", &mm);
    	
    	printf("Enter ending unit: \n");
    	printf("1 for millimeters\n");
    	printf("2 for centimeters\n");
    	printf("3 for meters\n");
    	printf("4 for kilometers\n");
    	printf("5 for inches\n");
    	printf("6 for feet\n");
    	printf("7 for yards\n");
    	printf("8 for miles\n");
    	scanf("%d", &end);
    	
    	/*Converts from mm*/
    	
    	switch(end){      
    
    		case 1:
    			ans = mm;
    			printf("%lf millimeters equals %lf millimeters\n", mm, ans);
    			fprintf(mmptr,"%lf mm = %lf mm\n", mm, ans);
    			strcpy(startunit[i],"mm");
    			strcpy(endunit[i],"mm");
    			strcpy(filename[i],"mm.txt");
            
    			break;
         
    		default:
    			printf("Not valid entry\n");
    			break;
            
    	i++;
        fclose(mmptr);
        }
              }
                    }
    
    		printf("Do you want to continue (y for yes)? ");
    		scanf("%s", check);
    		
    	}while(strcmp("y", check) == 0);
    	
    	
    	printf("Starting Unit\t\tEnding Unit\t\tFilename\n");
    
    for(i=0;i<20;i++){
            printf("%s", startunit[i]);
    	printf("%s", endunit[i]);
    	printf("%s", filename[i]);
    	             }
    	
        system("PAUSE");	
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > i++;
    Notice this statement is within your default case, so the only time i gets incremented is when "Not valid entry" is printed.

    Some other things:
    > startunit[i] == startunit[i+1];
    > endunit[i] == endunit[i+1];
    > filename[i] == filename[i+1];
    These statements basically don't do anything, as == is for equality. I'm not sure for what purpose you intended them.

    >for(i=0;i<20;i++){
    > printf("%s", startunit[i]);
    > printf("%s", endunit[i]);
    > printf("%s", filename[i]);

    Unless you actually have twenty entries, the unused entries probably will print garbage. Since you're incrementing i as you enter data, here you could say:
    for (j=0; j<i; j++)
    Of course, you probably also want to be sure during the first while-loop that i doesn't get incremented if "Not valid entry" is printed.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fprintf(mmptr,"%lf mm = %lf mm\n", mm, ans);
    %lf is really for scanning doubles. For printing doubles use %f. Some compilers will take %lf, but %f is more portable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 01:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21