Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Ask the user in C ? (http://www.go4expert.com/forums/user-c-t5297/)

coool 17Jul2007 05:31

Ask the user in C ?
 
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 !

DaWei 17Jul2007 05:48

Re: Ask the user in C ?
 
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).

coool 17Jul2007 05:55

Re: Ask the user in C ?
 
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 ?

DaWei 17Jul2007 06:00

Re: Ask the user in C ?
 
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.

coool 17Jul2007 06:31

Re: Ask the user in C ?
 
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

DaWei 17Jul2007 06:50

Re: Ask the user in C ?
 
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.

coool 17Jul2007 07:06

Re: Ask the user in C ?
 
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

coool 17Jul2007 07:24

Re: Ask the user in C ?
 
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 !! ..

DaWei 17Jul2007 07:52

Re: Ask the user in C ?
 
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.

coool 17Jul2007 08:25

Re: Ask the user in C ?
 
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
        }
}
*/



All times are GMT +5.5. The time now is 21:52.