Ask the user in C ?

coool's Avatar
Go4Expert Member
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 !
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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).
0
coool's Avatar
Go4Expert Member
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 ?
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
coool's Avatar
Go4Expert Member
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
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
coool's Avatar
Go4Expert Member
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
0
coool's Avatar
Go4Expert Member
isn't that nice

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 !! ..
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
coool's Avatar
Go4Expert Member
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 shabbir; 17Jul2007 at 09:17.. Reason: Code block