GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Mar-2004, 18:48
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

Another problem dealing with main() and driver function


I have another program dealing with a driver function. This is the problem: To see how main() works as a driver function, write main() as part of a complete program to call the function below:
CPP / C++ / C Code:
void draw_line(char what,int length)  /*function definition*/
{

	int ctr;

	for (ctr=0;ctr<length;ctr++)  /*Diver Function*/
		printf("%c",what);
}

So I wrote main() and called the function draw_line. The complete program is:
CPP / C++ / C Code:
#include <stdio.h>
#include <ctype.h>
#include <conio.h>

void draw_line(char what,int length);  /*function declaration */

int main()
{
	char c;		/*define the variables*/
	int x;

	c=3;		/*set the value of the input variables*/
	x=5;

	draw_line(c,x);		/*function call*/

	getch();

	return 0;		/*exit main function*/
}

void draw_line(char what,int length)  /*function definition*/
{

	int ctr;

	for (ctr=0;ctr<length;ctr++)  /*Driver Function*/
		printf("%c",what);
}
Now what I dont understand is why I am getting this weird output:
CPP / C++ / C Code:
if c=2, x=5;
☻☻☻☻☻ 
If c=3, x=5;
♥♥♥♥♥
if c=4, x=5;
♦♦♦♦♦ 
if c=5,x=5;
♣♣♣♣♣
if c=6,x=5;
♠♠♠♠♠
if c=7,x=5;
(My computer beeped and made a funny noise??)
if c=21, x=5;
§§§§§

Why am I getting such strange outputs? And did I write the program correct? After all, I did write main() and it's calling the function draw_line. Thanks for any feedback. I am still trying to learn functions and writing them for the "Driver" functions. Tommy
  #2  
Old 20-Mar-2004, 19:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by tommy69
c=3; /*set the value of the input variables*/

If c=3, x=5;
♥♥♥♥♥


Why am I getting such strange outputs? And did I write the program correct? After all, I did write main() and it's calling the function draw_line. Thanks for any feedback. I am still trying to learn functions and writing them for the "Driver" functions. Tommy


You want
Code:
c = '3';
for example.

the character 3 (0x03 hex) shows up as a heart on my Windows terminal.
the character '3' (0x33 hex) shows up as the digit '3'.

Dave
  #3  
Old 20-Mar-2004, 19:18
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Quote:
Originally Posted by davekw7x
You want
Code:
c = '3';
for example.

the character 3 (0x03 hex) shows up as a heart on my Windows terminal.
the character '3' (0x33 hex) shows up as the digit '3'.

Dave
Now that made me look stupid I say that because I should know better. I declared c as a character and I was trying to pass a number. I don't know why I didnt pick up on that. Sometimes the littlest mistakes just slips by. Thanks for bringing this little error up. I can change c to ='a' or '3' or anything I want as long as I have the ' ' surrounding the charatcer. Did I write the main() correctly? It seems to work fine to me. Thanks.....Tommy
  #4  
Old 20-Mar-2004, 19:18
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
As dave said...

What you did is output the value 2 which is the character ☻
3 is ♥
4 is ♦
etc.

These are in the lower 32 bytes of the ASCII table -- the CTRL characters
__________________

Age is unimportant -- except in cheese
  #5  
Old 20-Mar-2004, 19:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by tommy69
Did I write the main() correctly?

By George, I think you've got it!


Onward and upward!!!!

Dave
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:49.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.