473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

char and int input

maher5
7
hi

i wrote a program to convert int to binarys, it works just fine until the input is a char. how can i make it know the input is a char and not an int and therfore display and error msg?


it might be an easy q? but im only new to programming.

thanks a lot.
Oct 6 '07 #1
5 2452
Ganon11
3,652 Expert 2GB
Are you using C or C++?
Oct 7 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
It appears you have it working with an int. Yes?

If so, it should work with a char. Just move your chaer to an int either directly or use the char in the call to your function with an int argument.

I guess I'm not seeing the problem. A char is just another integer.
Oct 7 '07 #3
maher5
7
it is C

and my problem is when ever a char is entered the porgram would crash
thats my codings
note: it is not an assignment so i figured i can insert the codes


#include "stdafx.h"
int _tma
in(int argc, _TCHAR* argv[])
{
int input;
printf("NOTE: To Exit Enter 9999\n");
do {
printf ("Enter Int:\n");
scanf("%d", &input);
unsigned int mask =2147483;
int i=0;
for (i=0;i<32;i++) {
int itemp=0;
itemp = (input&mask);
if (itemp != 0) {
printf ("1");
}else {
printf ("0");
}
mask = mask/2;
}
printf("\n");
}while (input!=9999);

return 0;

}



thanks a lot
Oct 7 '07 #4
maher5
7
all i want is to be able to print an error msg whenever a char is entered and not crash.. thanks again
Oct 7 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
int input; <<<<<<
printf("NOTE: To Exit Enter 9999\n");
do {
printf ("Enter Int:\n");
scanf("%d", &input);
You bug is where the <<< are.

A char, like A is not an in integer value. scanf() expects integer values like 19 or -98. You would need to enter 65 instead to get the A.

If you change your input to a char it will work for characters but will now crash in integers.

scanf() assumes you know the format of the data before it is entered. If you can't tell whether a user will enter digits or characters then you can't use
scanf().

You have have to get a byte at a time and analyze the result.
Oct 7 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Q. John Chen | last post by:
It should be easy but I am not familier with javascript. I have a string that is encoded simply by replacing the letter with the next letter in the alphabeta. I need a function to convert it...
11
by: | last post by:
I repeatedly get into the situation where i need to take action accordingly to input in form of a char*, and have found two manners of approaching this, i'd appretiate pointers as to which is the...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
4
by: cdrom205 | last post by:
static void MDString ( unsigned char *input) { MD5_CTX context; unsigned char digest; unsigned int len = sizeof(input);//strlen (const char*) md5.MD5Init (&context); md5.MD5Update...
34
by: yaniv.dg | last post by:
hi all, i building an equition with float type now,what function should i use in order to identify is someone enter a char value by mistake?
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
20
by: gert | last post by:
This based on a example i found at http://www.cs.tut.fi/~jkorpela/ forms/cgic.html #include <fcgi_stdio.h> #include <stdlib.h> int urldecode(char *src, char *last, char *dest){ int code;...
7
by: dtschoepe | last post by:
Hi, I am working on a project for school and I am trying to get my head around something weird. I have a function setup which is used to get an input from stdin and a piece of memory is created...
7
by: william | last post by:
I wonder how 'main(int argc, char ** argv)' is implemented? How does it get the string literals separated by whitespace from the stdin stream? And is there any difference between 'char**' and...
0
by: steve.lorimer | last post by:
Thank you for taking the time to look at this: I'm looking for a pre-processor command that will allow me to resolve const strings into const char literals at compile time. Looking at the code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.