Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-22-2005, 02:22 PM   #1
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
Program in C

I found this program in the internet but I can`t make it work, there are some function that I don`t know what are they for, specially where it reads "cout <<", It also display an error with the library iostream.h This program reads numbers in binary, decimal, and hex, and prints them out in binary, decimal, and hex, any help will be appreciated.

// This program reads numbers in binary, decimal, and hex, and prints
// them out in binary, decimal, and hex.

#include conio.h
#include iostream.h
#include stdio.h
#include stdlib.h
#include ctype.h

// Functions to get the numbers in the various formats
int get_binary();
int get_hex();
int get_decimal();

// Print out the number in the various formats
void display(int);
void display_binary(int);
void display_hex(int);
void display_decimal(int);

// Explain how the program works
void help(void);

// Print the digits in binary or hex
void print_binary_digit(int);
void print_hex_digit(int);

// The main function
void main(void)
{
char c;
int x;
int done = 0;

cout << "\nRadix Converter\n";

// Print out the help function
help();

while(!done) {

// Get the command
cout << "Command: ";
c = getch();
cout << c << "\n";

switch(c) {
// binary input
case 'b':
cout << "Binary number: ";
x = get_binary();
display(x);
break;

// hex input
case 'h':
cout << "Hex number: ";
x = get_hex();
display(x);
break;

// decimal input
case 'd':
cout << "Decimal number: ";
x = get_decimal();
display(x);
break;

// help
case '?':
help();
break;

// quit
case 'q':
done = 1;
break;

// default case - print help
default:
help();
break;
}
}
}


// Print help
void help(void)
{
cout << "\nEnter d for decimal\n"
<< " h for hex\n"
<< " b for binary\n"
<< " q to quit\n"
<< " ? for help\n\n";

}

// Display the number in all three formats
void display(int x)
{
cout << "\n\n";
display_hex(x);
display_decimal(x);
display_binary(x);
cout << "\n";
}

// Convert a character to a number
int ctoi(char c)
{
switch(c) {
case '0': return(0);
case '1': return(1);
case '2': return(2);
case '3': return(3);
case '4': return(4);
case '5': return(5);
case '6': return(6);
case '7': return(7);
case '8': return(8);
case '9': return(9);
case 'a':
case 'A': return(10);
case 'b':
case 'B': return(11);
case 'c':
case 'C': return(12);
case 'd':
case 'D': return(13);
case 'e':
case 'E': return(14);
case 'f':
case 'F': return(15);
default: cout << "ctoi: error nonnumeric character\n"; break;
}
}


// Get a number in binary
int get_binary(void)
{
int bin = 0;
char c;

while(1) {
c = getch();

if(c == '0' || c == '1') {
cout << c;
bin = bin*2 + ctoi(c);
} else if(c == 13) {
return(bin);
}
}
}


// Get a number in decimal
int get_decimal(void)
{
int dec = 0;
char c;

while(1) {
c = getch();

if(isdigit(c)) {
cout << c;
dec = dec*10 + ctoi(c);
} else if(c == 13){
return(dec);
}
}
}


// Get a number in hexadecimal
int get_hex()
{
int hex = 0;
char c;

while(1) {
c = getch();

if(isxdigit(c)) {
cout << c;
hex = hex*16 + ctoi(c);
} else if(c == 13) {
return(hex);
}
}
}


// Display a number in binary
void display_binary(int x)
{
int i;
int nonzero = 0;

cout << "Binary: ";

for(i = 31; i >= 0; i--) {
nonzero += (x >> i) & 1;
if(nonzero)
print_binary_digit((x >> i) & 1);
}

if(x == 0)
print_binary_digit(0);

cout << "\n";
}


// Display a number in decimal
void display_decimal(int x)
{
cout << "Decimal: " << x << "\n";
}


// Display a number in hex
void display_hex(int x)
{
int i;
int nonzero = 0;

cout << "Hex: ";
for(i = 7; i >= 0; i--) {
nonzero += (x >> (i * 4)) & 0xF;
if(nonzero)
print_hex_digit((x >> (i * 4)) & 0xF);
}

if(x == 0)
print_hex_digit(0);

cout << "\n";
}


// Display a binary digit
void print_binary_digit(int x)
{
switch(x) {
case 0: cout << "0"; break;
case 1: cout << "1"; break;
default:
cout << "Error, print_digit accepts 0-15 only\n";
exit(-1);
break;
}
}


// Display a hex digit
void print_hex_digit(int x)
{
switch(x) {
case 0: cout << "0"; break;
case 1: cout << "1"; break;
case 2: cout << "2"; break;
case 3: cout << "3"; break;
case 4: cout << "4"; break;
case 5: cout << "5"; break;
case 6: cout << "6"; break;
case 7: cout << "7"; break;
case 8: cout << "8"; break;
case 9: cout << "9"; break;
case 10: cout << "A"; break;
case 11: cout << "B"; break;
case 12: cout << "C"; break;
case 13: cout << "D"; break;
case 14: cout << "E"; break;
case 15: cout << "F"; break;
default:
cout << "Error, print_hex_digit accepts 0-15 only\n";
exit(-1);
break;
}
}

Last edited by easg; 06-22-2005 at 02:25 PM.
easg is offline   Reply With Quote
Old 06-22-2005, 02:25 PM   #2
RJ
Member (14 bit)
 
Join Date: Feb 2000
Location: Offenbach/Main (Germany)
Posts: 8,485
Send a message via ICQ to RJ
cout does not exist in C, it was added in C++. Since C++ was developed from C, they are pretty similar, so you can alter the code to make it 100% C (for example, you need to replace the cout lines by printf lines). .. or you get a C++ compiler.

EDIT: I see you posted now the entire program. Actually I started with C++, never programmed in C, but I think it might be enough to get rid of iostream.h and replace the cout lines by their printf counterparts, to make that C++ program a C program.

RJ
__________________
All's right with the world when your PC is working right.

Last edited by RJ; 06-22-2005 at 02:30 PM.
RJ is offline   Reply With Quote
Old 06-22-2005, 03:29 PM   #3
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
Ok I will try that and post the results, I have never study C++ only C.
easg is offline   Reply With Quote
Old 06-22-2005, 03:41 PM   #4
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
Ok I compiled the program with Visual C++ 6.0 and it displayed this --------------------Configuration: Conversor - Win32 Debug--------------------
Compiling...
Conversor.cpp
c:\documents and settings\eduardo\escritorio\conversor.cpp(137) : warning C4715: 'ctoi' : not all control paths return a value

Conversor.obj - 0 error(s), 1 warning(s)

what does it means?
easg is offline   Reply With Quote
Old 06-22-2005, 04:18 PM   #5
RJ
Member (14 bit)
 
Join Date: Feb 2000
Location: Offenbach/Main (Germany)
Posts: 8,485
Send a message via ICQ to RJ
It means that there is a possibility that the function ctoi, which is defined to return an int value, sometimes does not return a value.
Take a look at the function: it consists of the switch block, and for the cases that c contains a character from 0 to F, ctoi returns an int value.
In any other case, an error message is displayed, but to value is returned, and then the lines like "hex = hex*16 + ctoi(c);" that use ctoi for calculating, won't work.

This isn't a problem as long as the variable c contains a character for which ctoi returns a value, either by typing in the correct character, or by filtering (like: not accepting anything else than 0 - F, so that ctoi will return a value).

Since that problem only affects certain cases, and is not a problem that prevents the program from running, you'll get a warning instead an error message.

RJ

Last edited by RJ; 06-22-2005 at 04:22 PM.
RJ is offline   Reply With Quote
Old 06-22-2005, 04:24 PM   #6
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
Ok I builded the exe file but it doesn`t work when I run it, can you test it to see what is going on? It didn`t displayed any error during the creation of the file.
easg is offline   Reply With Quote
Old 06-22-2005, 04:27 PM   #7
RJ
Member (14 bit)
 
Join Date: Feb 2000
Location: Offenbach/Main (Germany)
Posts: 8,485
Send a message via ICQ to RJ
Sure. I too have Visual C++ 6.0, and the exe didn't display anything at the beginning. I'll have to look at the code to understand how it's supposed to work, and when I find anything, I'll post again.

EDIT:
Ok, the problem is the buffer that is used for stream output (cout). You can solve the problem by writing

cout << flush;

in front of every c=getch(); line (I think there are 3 of them), to force the flush of the buffer.
Or, you do the following. You include iomanip.h and write

cout << setiosflags(0x2000);

right at the beginning of the main function. That will force the flush of the buffer for the entire program.

RJ

Last edited by RJ; 06-22-2005 at 05:12 PM.
RJ is offline   Reply With Quote
Old 06-24-2005, 10:42 AM   #8
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
Problem solved, thanks RJ
easg is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:52 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2