Thread: Help- Save inputs of data and result use array

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    34

    Help- Save inputs of data and result use array

    So here my problem is i cannt figure out how to save the input that the user inserted before and it will be displayed when user dont want to continue the process.. below is my code.. when user select the choice for example 1.. and user will input the straight line.. and after that user willing to input another selection .. let say 2 and user input the rectangle.. once done user dont want to continue the process and wanted to end the program and it will showing the summary of the input and result that user had entered before... sound pretty easy.. but i didnt know where i want start

    Code:
    #include<stdio.h>
    #include<cstdlib>
    int main()
    {
        int num,height;
        char selection;
    
            
            printf("\n\t\t___________________________________________");
            printf("\n\t\t-Please chose your selection between 1 to 4 -");
            printf("\n\t\t___________________________________________");
            printf("\n\t\t|    1 : Straight Line          |");
            printf("\n\t\t|    2 : Rectangle              |");
            printf("\n\t\t|    3 : Triangle              |");
            printf("\n\t\t|    4 : Diamond              |");
            printf("\n\t\t___________________________________________\n");
            
            printf("\n\t\tYour selection : ");
            scanf("%c", &selection);
        
            switch (selection){
            case '1':
                {
                    printf("\n\nEnter the length of * : ");
                    scanf("%d", &num);
                    for(int row=1; row<=num;row++)
                {
                    printf("* ");
                }
                    printf("\n\n");
                    printf("Number of * is : %d \n\n",num);
                    break;
                }
    
            case '2' :
                {
                int length, width;
                double area;
                    printf("Please enter the length of Rectangle :");
                    scanf("%d", &length);
                    printf("\nPlease enter the width of Rectangle :");
                    scanf("%d", &width);
    
                    int column = 0;
                    while(column < length)
                {
                    column++;
                        int row = 0;
                        while(row <= width)
                    {      
     
                        if (((row > 1)&&(row < width))&&((column > 1)&&(column < length)))
                    {
                            printf("  ");
                        }
                            else if((row !=0 )&&(column!=0))
                        {
                            printf("* ");
                        }
     
                            row++;
                        }
                            printf("\n");         
     
                        }
                            printf("\n");
                            area = length * width;
                            printf("%d * %d = %.2f\n\n",length,width,area);
                            printf("Area of rectangle is: %.2f\n\n",area);
                            break;
                        }
    
            case '3':
                {
                    int num,h,i,j;
                    double AreaOfTriangle;
                        printf("\nPlease enter the height of triangle: ");
                        scanf("%d", &num);
                        printf("*\n");
                    for(h=1; h<=num-2;h++)
                {
                        printf("* ");
                    for(j=1; j<h; j++)
                        printf("  ");
                        printf("* \n");
                }
                    for(i=1; i<=num; i++)
                        printf("* ");
                        printf("\n\n");
                    AreaOfTriangle = (num*num)/2;
                    printf("(%d*%d)/2 = %.2f\n\n",num,num,AreaOfTriangle);
                    printf("The area of triangle is %.2f: \n\n",AreaOfTriangle);
                    break;
                }
            case '4':
                {
                double area;
                printf("\nEnter the height of Diamond :");
                scanf("%d", &height);
                for(int i=-height;i<=height;i++)
                    {
                    for(int j=-height; j<=height; j++)
    
                {
    
                if( abs(i)+abs(j)==height - 1)
    
                    { 
                        printf("*");
                }
    
                else 
                { 
                printf(" ");
                }
                }
    
                printf("\n");
                }
                area = 2*(height*height);
                printf("Area of diamond is: %.3f\n",area);
                break;
                }
            default:
                {
                printf("\nOut Of Selection!!\n");
                return 0;
                }
        
                    
        }    
        
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not totally clear on what this "summary" is. Perhaps you could provide a clear example. For example, if the user enters 1 line of length 5, then a rectangle that ix 5x7 and another rectangle that is 3x8, what should the summary be?

    Also, if you need your program to repeat, you need to enclose it in a loop. A do-while loop is good, since it always executes at least once (you always want to ask the user what they want to do at least once before quitting). Something like:
    Code:
    do {
        print menu
        get selection
        switch (selection)
            case line:
            case rectangle
            ...
        print "Would you like to draw another shape (y/n)?"
        get another_shape
    } while (another_shape is 'y' or 'Y');

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    the summary that i mean is once user finished input 2 type of selection that selected and he end the loop and lastly it will display all the input for each shape at the end.. it example it would be look like this.. >> http://prntscr.com/kzevn

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, so you need to save all the user input. Is there a maximum you can enforce, like the user can input at most 10, or 100 shapes? Or can you ask the user how many shapes they want ahead of time? If so, then an array would work. If not, you need a dynamic data structure like a linked list (which seems too advanced for the assignment).

    Either way, the do while loop is still the best way to handle repeated user input, but you would have to move the drawing code to a separate loop. So first, you have a loop that gets all the shapes the user wants to draw, and save the type of shape and it's dimension(s) to the array/linked list. Make sure to keep track of how many shapes they actually entered. Then, in the second loop, you iterate though each shape shape the user wanted and print it out.

    You should move the drawing code for each shape it's own function: void draw_line(int length), void draw_rect(int length, int width), etc. That will make your program much neater and easier to follow, modify, debug, fix, etc.

    Lastly, note that #include<cstdlib> is actually a C++ include, but I'm guessing you are supposed to be programming in C, so replace it with #include<stdlib.h>.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    let me repeat and understand what are u said.. so example i gonna using arrays to save the input values like shape option, width and height of the shape.. let say user gonna save 50 shape.. its gonna declare smth like this >> int save[50][3]; let said 1st colum contain the option shape, 2nd and 3rd colum contain width and height.. uhmm did the array will be coded inside case statement?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Some shapes need one number (line, triangle, diamond) and some need two (rectangle). The shape is coded as 1, 2, 3, or 4. So one suggestion is to uses an array of N x 3 elements. Each row of the array will be shape code, height, and width where applicable.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by aquilina View Post
    let me repeat and understand what are u said.. so example i gonna using arrays to save the input values like shape option, width and height of the shape.. let say user gonna save 50 shape.. its gonna declare smth like this >> int save[50][3]; let said 1st colum contain the option shape, 2nd and 3rd colum contain width and height.. uhmm did the array will be coded inside case statement?
    Yes, you're on the right track, you understand quite well. As nonoob pointed out, not every shape needs two dimensions, so you just wont use one of the array elements in some cases.

    I'm not totally sure what you mean about the "array coded inside case statement", but you should declare it at the top of main. Assuming you store the shape type in array[i][0], you will use switch (array[i][0]) to determine which shape to draw.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    btw i already get the flow of the program.. so how the syntax for the switch case with array will be looked like ?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by aquilina View Post
    btw i already get the flow of the program.. so how the syntax for the switch case with array will be looked like ?
    We don't like to just hand out solutions around here. I gave you a big hint in my last post. [edit]You already know the general format of using switch statements.[/edit] Give it a shot, try it yourself. If you run into trouble, post your updated code with a clear problem description, and we'll give you a hand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using cat to putput data and save
    By bigmac(rexdale) in forum Linux Programming
    Replies: 8
    Last Post: 12-17-2007, 08:29 AM
  2. save data in array
    By carlyn in forum C Programming
    Replies: 2
    Last Post: 04-10-2007, 06:09 AM
  3. How To Save Dynamic Data
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 11-25-2003, 10:41 AM
  4. Save data
    By jkw2068 in forum C++ Programming
    Replies: 3
    Last Post: 06-20-2003, 06:04 PM
  5. Save data
    By webguy899 in forum C Programming
    Replies: 3
    Last Post: 05-02-2002, 08:03 PM