Thread: C, Question About My program. Just Starting

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Question C, Question About My program. Just Starting

    Hey guys!


    I am making a program for my c class.
    sample run is:
    ~~~ CALCULATOR ~~~
    > x:3/4
    > y:12/5
    > # multiply
    > z=x*y
    > # print z to get 9/5
    > z
    9/5
    > x:6/5
    > y=x+z
    > # add to get 3
    > y
    3
    > # quit
    > q
    ~~~ GOODBYE ~~~

    ok, this is a little hard to explain. This program is like a command prompt, it uses ">" and then the user enters data.
    since i just started, in am in the beginning of it. i need to prompt the user for input with ">" My teacher wants us to use pointers and functions. (no arrays and strings).


    I am not sure if i am doing this right, and i stuck already..
    This is a function below:
    Code:
     
    void input (void)
    {
     char uInput;
     do{ 
        printf("> ");
        scanf("%c", &uInput);
       } while (uInput != 'q');  
    }
    Calling Function with main...
    Code:
    int main (void)
    {
     printf("~~~ CALCULATOR ~~~\n");
     input(); 
     return 0;
    }

    Whatever I entered are random for now...
    I am getting the output as:
    > 3/4
    > > y
    > >
    Why am i getting "> >" ? help me please.

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Anyone?

    i just need a function which prints "> " and takes users input (char, int) whenever the user presses <enter> (\n) it has to go to new line and print "> " again.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Regarding this question:

    Why am i getting "> >" ? help me please.
    Look at your "do-while" loop. What are you telling the program to do in that loop?

    1. print arrow
    2. read a single character
    3. if that character is not 'q', then go back to #1

    Do you see what's happening? What will your program do if you enter several characters at once? You need a minor re-structuring of code in that function.

    Onto the bigger issue:

    My teacher wants us to use pointers and functions. (no arrays and strings).
    Are you sure you understand the requirements and limitations of your assignment? Based on the sample you've provided, this would be obscenely difficult - nay, downright futile - to implement. You might want to clarify the requirements.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Post more info

    Quote Originally Posted by Matticus View Post
    Regarding this question:

    Look at your "do-while" loop. What are you telling the program to do in that loop?

    1. print arrow
    2. read a single character
    3. if that character is not 'q', then go back to #1

    Do you see what's happening? What will your program do if you enter several characters at once? You need a minor re-structuring of code in that function.

    Onto the bigger issue:

    Are you sure you understand the requirements and limitations of your assignment? Based on the sample you've provided, this would be obscenely difficult - nay, downright futile - to implement. You might want to clarify the requirements.
    C, Question About My program. Just Starting-assignment2-jpg

    http://postimage.org/image/wz3u2702f

    That is the requirements.
    I have moved on a little, but still i am facing problem.
    for now i am not starting the custom cmd prompt ("> ") , i will put that after.

    now i am testing a new function for the first two input

    Code:
    #include <stdio.h>
    
    void firstInput (void)
    {
     int x, y, *pNum1, *pDem1;
     char letter1, *pVar1;
     pVar1 = &letter1;
     pNum1 = &x;
     pDem1 = &y;
    
    
     printf("> ");
     scanf ("%c:%d/%d", &letter1, &x, &y);
     printf( "%d/%d is  %c\n", *pNum1, *pDem1, *pVar1);
    }
    void secondInput (void)
    {
     int a, b, *pNum2, *pDem2;
     char letter2, *pVar2;
     pVar2 = &letter2;
     pNum2 = &a;
     pDem2 = &b;
    
    
     printf("> ");
     scanf ("%c:%d/%d", &letter2, &a, &B)/>/>/>;
     printf( "%d/%d is  %c\n", *pNum2, *pDem2, *pVar2);
    }
    
    
    int main (void)
    {
     printf("~~~ CALCULATOR ~~~\n");
     firstInput();
     secondInput();
     return 0;
    }
    OUTPUT:
    ~~~ CALCULATOR ~~~
    > X:3/4
    3/4 is X
    > Y:12/5
    3/4 is
    I don't know what i am doing wrong. I am totally lost.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Post next step to take??

    Quote Originally Posted by Salem View Post
    Thank you!

    Now i am able to do 30% of the program.
    Code:
    #include <stdio.h>
    void printcmd (void)
    {
     printf("> ");
    }
    void firstFraction (char letter1)
    {
     int x, y, *pNum1, *pDem1;
     char *pVar1;
     pVar1 = &letter1;
     pNum1 = &x;
     pDem1 = &y;
     printcmd();
     scanf (" :%d/%d",&x, &y);
    // printf( "%d/%d is  %c\n", *pNum1, *pDem1, *pVar1);
    }
    void secondFraction (void)
    {
     int x, y, *pNum2, *pDem2;
     char letter1, *pVar2;
     pVar2 = &letter1;
     pNum2 = &x;
     pDem2 = &y;
     printf("> \b\b");
     scanf (" %c:%d/%d", &letter1, &x, &y);
    // printf("%d/%d is  %c\n", *pNum2, *pDem2, *pVar2);
    }
    
    
    void comment (void)
    {
     char junk;
     do
     {
       scanf("%c", &junk);
     } while (junk != '\n');
    }
    
    
    int main (void)
    {
     char first_char;
     printf("~~~ CALCULATOR ~~~\n");
     printcmd();
     do {
     scanf("%c", &first_char);
     if (first_char <= 122 && first_char >= 97)
     {
        firstFraction(first_char);
        secondFraction();
     }
     printcmd();
     if (first_char == 35)
     {
       comment();
     }
     }while (first_char != 'Q');
     printf("\b\b~~~ GOOD BYE ~~~\n");
     return 0;
    }
    Sample run:
    ~~~ CALCULATOR ~~~
    > # testing the comment function
    > # it works
    > # now to test x:... fraction function
    > x:3/4
    > y:12/5
    > > # it workd
    > # now to test Quit cmd 'Q'
    > Q
    ~~~ GOOD BYE ~~~
    Now i am on the > z=x*y place.

    i need to make a function, where "z" can be anything entered x*y will multiply.
    I dont know much about pointers. learned what are pointers yesterday .
    I need help in setting variable "z" and a function for multiplying the two fractions.
    My question is when i create a new function, lets say "
    multiply". how do i use data from other functions? like the firstFraction an secondFraction.


  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Am I using the pointers the right way? how do i retrieve data from the function firstFraction (*pNum1 and *pNum2...)
    Please help me. I only have 4 days left to do this assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting a new program
    By histevenk in forum C++ Programming
    Replies: 28
    Last Post: 10-15-2007, 04:11 PM
  2. starting program
    By Ideswa in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2006, 02:36 PM
  3. Starting a Program, use C or C++?
    By firestorm in forum Windows Programming
    Replies: 12
    Last Post: 04-11-2005, 09:07 PM
  4. Starting the program
    By vasanth in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 04:51 AM
  5. Replies: 2
    Last Post: 11-08-2001, 08:42 PM

Tags for this Thread