Ask the user in C ?

Discussion in 'C++' started by coool, Jul 17, 2007.

  1. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    hey guys..

    using C language,

    how can I ask the user to enter a positive integer

    in only these forms --> binary, octal, decimal, or hexadecimal

    then save it in a charachter array !
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Use fgets or scanf. Your user will have to specify the form in those cases where the input is ambiguous (e.g. 12345 might be octal, decimal, or hexadecimal, all of which have different values for that set of digits).
     
  3. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    after asking the user for the positive number..

    I'm going to ask him again what's the base of this positive number..

    and from here I can tell if the form is octel or decimal or .. etc ! ..

    scanf I can't use ! .. neither getchar() -- to avoid a known C bug.. cuz I'm dealing with a char..

    what do you think ! .. am I wrong ?
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Known C bug? Please do elaborate.

    I don't think you're wrong, I just think you're uniformed. Break a sweat and post some code; we'll help you out.
     
  5. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    okay.. this is the code I've done until now.. :)

    I'm just testing here ! .. as I'm still a biggenner in C programming..

    Code:
    #include <stdio.h>
    #include <string> // make standard strings available
    #include <iostream> // make standard I/O available
    
    int main()
    {
    	using namespace std;
    	string name;
    
    	cout << "Please enter your name: ";// prompt the user
    	cin >> name;// read a name
    	cout << "Hello " << name << "..\n"; //output the name followed by a new line
    }
    
    Error:
    c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error : "eh.h is only for C++!"

    I'm compiling my code in Microsoft Visial c++ 6
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You need to decide whether you are programming in C or in C++. They are not the same language. Your error message indicates to me that you have named your file "something.c", which means the compiler will compile it as C. It will not like the string class or C++ I/O (cout, cin).

    If you decide to write it as C++, then you will need to change the file's extension. You will also not want to include stdio.h. There's really no reason to include it, since you aren't using an C-style I/O (printf, scanf, fgets, etc.), but if you wanted to use C-style I/O, you would include cstdio, not stdio.h. This places those functions in the standard namespace.

    If you were writing anything but a trivial program, you would not want to use "using namespace standard;" That statement brings about lebenty-zillion symbols into your program. With much code at all, you're almost bound to duplicate one and cause yourself much grief.

    Decide which language you're using, and try again.
     
  7. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    ahaaaaaaa

    I just noticed.. that cout and cin are for c++ language... but printf,scanf, and fgets are for c language..

    I want to code in C language :)

    I'm not sure if I need to use "using namespace standard", we will see as we go on :)

    I'll try again.. I'll post something in few minutes :D
     
  8. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    isn't that nice :D

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char posNumChar[30];
    
        printf("Please enter positive number: ");
    	
        fgets(posNumChar, 30, stdin);
    
        printf("You entered this positive number, %s", posNumChar);
    
        getchar();
    }
    
    now I want to do the second step.. which is asking the user for the base of the number he entered..

    I've been asked to avoid using scanf() to a character and using getchar() --- IN THIS STEP only !

    is there any other functions that work the same way as scanf and getchar !! ..
     
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Getchar is okay, but it's going to leave stuff in the input buffer (a newline, at the very least). That will still be there to plague you on any subsequent input requests. You will have to learn how to get rid of superfluous junk like that.

    Personally, I would use fgets to get the entire line. If I were feeling nice, I'd then just take the first character and see if it matched any of the characters I had specified as representatives of a base. If you're specifying that the base be expressed as a number, then if you use getchar you are going to have to collect then convert the input. If you use fgets, then you can use sscanf to collect and convert the input. If your master is telling you not to use any form of scanf, then he/she is telling you that you need to learn how to make your own numerical conversions.

    No matter what you use to collect input, always test to see if the input succeeded. Users do not react as you think, they react according to whether they're inattentive, lazy, malicious, or just asleep with their forehead on the keyboard. Failing to test input is the sign of an uninformed person, or a schlock. You are now informed, so there's only one conclusion to draw if you fail to test it hereafter.

    The std namespace is a C++ thangy. Don't try to use it in a C program.
     
  10. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    This isn't working ! :(

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char input[30];
    	int base;
    
        printf("Please enter positive number: ");
        fgets(input, 30, stdin);
    
    	printf("Please ener the base (1->binary, 2->octal, 3->decimal, 4->hexadecimal): ");
    	fgets(base,1,stdin);
    
        //Check(input,base);
    }
    
    /*
    int Check(char input[], int base)
    {
    	switch (base)
    	{
    	case 1://binary
    		//statment
    	case 2://octal
    		//statment
    	case 3://decimal
    		//statment
    	case 4://hexadecimal
    		//statment
    	}
    }
    */
     
    Last edited by a moderator: Jul 17, 2007
  11. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    First of all, remember to put your own code in code tags, instead of poor Shabbir having to do it. He ain't yo' mama.

    What you get from input is going to be a character (multiple characters, actually, a string), not an integer. That's why I mentioned conversions. Give this a shot (note the cases are characters, not integers):
    Code:
    #include <stdio.h>
    
    int ohHell (char *trouble)
    {
        fprintf (stderr, "%s\n", trouble);
        return EOF;
    }
    int main()
    {
    	char input[256];
    	char base [256];
    
        printf("Please enter positive number: ");
        if (!fgets (input, 30, stdin)) return ohHell ("Failure on input");
    
    	printf("Please ener the base (1->binary, 2->octal, 3->decimal, 4->hexadecimal): ");
        if (!fgets (base, 30, stdin)) return ohHell ("Failure on input");
    
        printf ("You selected ");
        switch (base[0])
        {
        case '1':
            printf ("binary");
            break;
        case '2':
            printf ("octal");
            break;
        case '3':
            printf ("decimal");
            break;
        case '4':
            printf ("hex");
            break;
        default:
            printf ("an invalid character");
        }
        printf ("\n");
        return 0;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice