Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-13-2007, 07:44 PM
siren siren is offline
Newbie
 
Join Date: Apr 2007
Posts: 10
Rep Power: 0
siren is on a distinguished road
Question Please Help With A C Program!!

Hello everyone! I just started to learn how to program using C, and was given an assignment I cannot solve. I really need help

I have to create a program that
1) changes an ASCII code entered by the user into the appropriate letter.
2)changes a character entered in by the user into ASCII code
3) Display an ASCII TABLE
4) and when the letter "q" is entered, the program ends...

I'm sorry it may be a little confusing, but this is what I was able to come up with so far.

Code:
#include <stdio.h>

int main(){

  int i;
  char c,type;

  printf("ASCII character program\n\n");
  printf("Input 1, 2, 3, or q\n");
  printf("1: ASCII code -> CHAR\n");
  printf("2: CHAR -> ASCII code\n");            *This is the menu thats
  printf("3: DISPLAY ASCII TABLE\n");            suppose to loop after each
  printf("q: End of this Program\n");               choice and the answer*
  scanf("%d",&type);
  
  while(type!=113){
    if(type=49){
      printf(" ASCII code -> CHAR \n");
      printf(" input ASCII code\n");
      scanf("%d",&i);
      printf(" Character for ASCII code %d is -> '%c'\n\n", i,i);}
  
    if(type=50){
      printf(" CHAR -> ASCII code \n");
      printf(" input a Character\n");
      scanf("%c",&c);
      printf(" ASCII code for character '%c' is -> '%d'\n\n", c,c);}

  
    if(type=51){
      printf("ASCII code table for 1-127\n");
      for(i=1; i<=127; i++)
    printf("Character for ASCII code %d is -> '%c'\n",i,i);}
}
}
I need it to keep repeating the menu at the top, until the user enters "q" after the menu. Sorry for the unclearness but any comment will be helpful.. thanks in advance.

Last edited by Jordan; 04-13-2007 at 07:49 PM. Reason: Added code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-13-2007, 07:51 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 4,856
Last Blog:
Zend Studio for Eclips...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Add another while loop at the top

Code:
while (type=..)
Replace the .. with whatever the ascii value of q equates to.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-13-2007, 09:25 PM
siren siren is offline
Newbie
 
Join Date: Apr 2007
Posts: 10
Rep Power: 0
siren is on a distinguished road
Default

thank you Jordan for the quick reply! I tried out your advice but I'm still having some difficulties...

I want it to work out like this..

ASCII character program
Input 1,2,3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: Display ASCII TABLE
q: End of this program
1
input ASCII code
123
Character for ASCII code 123 is -> '{'
Input 1,2,3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: Display ASCII TABLE
q: End of this program
2
input a character
r
ASCII code for character 'r' is -> 114
Input 1,2,3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: Display ASCII TABLE
q: End of this program
q

END

This is just an example of how I want the program to work, but I can't seem to get the looping part right...please HELP!!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-14-2007, 03:15 AM
v0id's Avatar   
v0id v0id is online now
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,004
Last Blog:
CherryPy(thon)
Rep Power: 21
v0id is just really nicev0id is just really nicev0id is just really nicev0id is just really nice
Send a message via MSN to v0id
Default

You're using the assignment-operator, when you're comparing the user-input, with the options to choose. You need to use the compare-operator instead. To optimize the code, I would prefer to use a switch-statement, instead of if's (there by the way, should have been if...elseif...else).
Code:
#include <stdio.h>

/* This is just a little thing, to make our application nicer */
#define SEPARATOR "------------------------------------\n"

int main()
{
	int i, c, type;
	
	type = 0; /* We need to initialize the type-variable */
	
	/* There isn't a big difference on int's and char's, so we don't  */
	/* need to use the ASCII-code, we can just you the real character */
	while(type != 'q')
	{
		printf("ASCII character program\n\n");
		printf(" Input 1, 2, 3, or q\n");
		printf("  1: ASCII code -> CHAR\n");
		printf("  2: CHAR -> ASCII code\n");
		printf("  3: DISPLAY ASCII TABLE\n");
		printf("  q: End of this Program\n");
		printf(" >> ");
		
		/* I see in your if-statements, you're using the ASCII-codes, */
		/* that means, we've to scan the _character_ using %c         */
		scanf("%c", &type);
		
		/* Separator */
		printf("%s", SEPARATOR);
		
		/* Flush everything in STDIN, so it don't keep it, */
		/* until the switch                                */
		fflush(stdin);
		
		/* This is our switch-statement */
		switch(type)
		{
			/* If the user choose '1' */
			case '1':
				printf("ASCII code -> CHAR\n");
				printf(" Input ASCII code: ");
				scanf("%d", &c);
				printf("  Character for ASCII code %d is -> '%c'\n\n", c, c);
				break;
				
			/* If the user choose '2' */
			case '2':
				printf("CHAR -> ASCII code\n");
				printf(" Input CHAR: ");
				scanf("%c", &c);
				printf("  ASCII code for character '%c' is -> '%d'\n\n", c, c);
				break;
				
			/* If the user choose '3' */
			case '3':
				printf("ASCII code table for 1-127\n");
      			for(i = 1; i <= 127; i++)
    				printf("Character for ASCII code %d is -> '%c'\n", i, i);
				break;
			
			/* If the user choose non of above */
			default:
				printf("Invalid option, try again...\n");
		}
		
		/* We need to flush again, before next round */
		fflush(stdin);
		printf("%s", SEPARATOR);
	}
}
That's how I would do it. As you can see it's using a switch-statement instead of the if-statements. It's usually a good idea to use a switch-statement, if there's many options. But there isn't so many in this one, so we could had used if...elseif...else-statement.

By the way, which compiler are you using? I've never seen comments like this;
Code:
int var = 10; * This is a comment *
Like you're using in your code. It isn't standard C, so stop using it.
Code:
C:
/* COMMENT */

C++:
// COMMENT
/* COMMENT */
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-15-2007, 09:49 AM
siren siren is offline
Newbie
 
Join Date: Apr 2007
Posts: 10
Rep Power: 0
siren is on a distinguished road
Default

thanks again for all the help void!! I tried out your code and it worked out very well...except there seems to be some problems... First of all, I really couldn't use codes such as fflush and so on. I've only learned the real basics such as looping via if, and so on so I really wasn't allowed to use all those advanced codes...sry I didn't mention that before hand...So I used void's program as a reference and fixed up my program, but it just isn't working... this is what I did...

#include <stdio.h>


int main()
{
int i,type;
char c;
type = 0;

printf("ASCII character program\n\n");
while(type!='q')
{
printf(" Input 1, 2, 3, or q\n");
printf(" 1: ASCII code -> CHAR\n");
printf(" 2: CHAR -> ASCII code\n");
printf(" 3: DISPLAY ASCII TABLE\n");
printf(" q: End of this Program\n");
printf(">>");
scanf("%c", &type);


if(type=='1')
{
printf("ASCII code -> CHAR\n");
printf(" Input ASCII code: ");
scanf("%d", &c);
printf(" Character for ASCII code %d is -> '%c'\n\n", c, c);
}

else if(type=='2')
{
printf("CHAR -> ASCII code\n");
printf(" Input a character: ");
scanf("%c", &c);
printf(" ASCII code for character '%c' is -> '%d'\n\n", c, c);
}

else if(type=='3')
{
printf("ASCII code table for 1-127\n");
for(i = 1; i <= 127; i++)
printf("Character for ASCII code %d is -> '%c'\n", i, i);
}


}}

The result of this code was so...
$ ./a.exe
ASCII character program

Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>>1
ASCII code -> CHAR
Input ASCII code: 123
Character for ASCII code 123 is -> '{'
Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>> Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>>2
CHAR -> ASCII code
Input a character: ASCII code for character '
' is -> '10'

Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>>q

command 1, 3, and q work perfectly....however 2: CHAR -> ASCII code just isn't working... it won't let me enter the character to convert...I can't figure it out....another problem was that it shows the menu twice after each attempt...please help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 04-15-2007, 11:57 AM
v0id's Avatar   
v0id v0id is online now
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,004
Last Blog:
CherryPy(thon)
Rep Power: 21
v0id is just really nicev0id is just really nicev0id is just really nicev0id is just really nice
Send a message via MSN to v0id
Default

First of all, try learn to use the [ code ]-tags.

The problems you have is because you don't use the fflush()-function. When something is entered through the input-stream (stdin) during execution of the application, it will mostly stay in there. When it then tries to get some new stuff through the stream, it will sometimes be... yeah, strange. When using fflush() and stdin together, the stdin will be cleared and you can retrieve new input, without worry.

I've made minor changes to your application, actually I've only inserted the fflush()-function, two times. I've commented the code those placed, so you get what's going on.
Code:
#include <stdio.h>

int main()
{
	int  i, type;
	char c;
	
	type = 0;
	
	printf("ASCII character program\n\n");
	while(type != 'q')
	{
		printf(" Input 1, 2, 3, or q\n");
		printf(" 1: ASCII code -> CHAR\n");
		printf(" 2: CHAR -> ASCII code\n");
		printf(" 3: DISPLAY ASCII TABLE\n");
		printf(" q: End of this Program\n");
		printf(">>");
		scanf("%c", &type);
		
		/*\
		|*|		First we get some input from the user, into 
		|*|		_type_, through the stdin (Standard Input Stream).
		|*|		Then, before we're retrieving new data
		|*|		(in the if-statement) we have to clear it.
		|*|		This is done with fflush().
		\*/
		fflush(stdin);
		
		if(type == '1')
		{
			printf("ASCII code -> CHAR\n");
			printf(" Input ASCII code: ");
			scanf("%d", &c);
			printf(" Character for ASCII code %d is -> '%c'\n\n", c, c);
		}
		else if(type == '2')
		{
			printf("CHAR -> ASCII code\n");
			printf(" Input a character: ");
			scanf("%c", &c);
			printf(" ASCII code for character '%c' is -> '%d'\n\n", c, c);
		}
		else if(type == '3')
		{
			printf("ASCII code table for 1-127\n");
			for(i = 1; i <= 127; i++)
				printf("Character for ASCII code %d is -> '%c'\n", i, i);
		}
		
		/*\
		|*|		Now we got something into _c_, and before
		|*|		the loop starts over again, we need to clear
		|*|		it before we again will retrieve something in _type_.
		\*/
		fflush(stdin);
	}
	
	return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-17-2007, 08:37 AM
siren siren is offline
Newbie
 
Join Date: Apr 2007
Posts: 10
Rep Power: 0
siren is on a distinguished road
Default Void I need help!!

Void, I actually tried out ur code, exactly as written here, and well, it just isnt working... its still having the same problem...


I've been working on this program for the past few days, and well....it just isn't working out at all...It's starting to get really annoying..

Code:
#include <stdio.h>
int main()
{
	int  i, type;
	char c;
	type = 0;
	
	printf("ASCII character program\n\n");
	while(type != 'q')
	{
		printf(" Input 1, 2, 3, or q\n");
		printf(" 1: ASCII code -> CHAR\n");
		printf(" 2: CHAR -> ASCII code\n");
		printf(" 3: DISPLAY ASCII TABLE\n");
		printf(" q: End of this Program\n");
		printf(">>");
		scanf("%c", &type);
		
		fflush(stdin);
		
		if(type == '1')
		{
			printf("ASCII code -> CHAR\n");
			printf(" Input ASCII code: ");
			scanf("%d", &c);
			printf(" Character for ASCII code %d is -> '%c'\n\n", c, c);
		}
		else if(type == '2')
		{
			printf("CHAR -> ASCII code\n");
			printf(" Input a character: ");
			scanf("%c", &c);
			printf(" ASCII code for character '%c' is -> '%d'\n\n", c, c);
		}
		else if(type == '3')
		{
			printf("ASCII code table for 1-127\n");
			for(i = 1; i <= 127; i++)
				printf("Character for ASCII code %d is -> '%c'\n", i, i);
	            }
		
		}
	
	return 0;
}

AND I want the program to run like so...
ASCII character program
Input 1,2,3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: Display ASCII TABLE
q: End of this program
1
input ASCII code
123
Character for ASCII code 123 is -> '{'
Input 1,2,3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: Display ASCII TABLE
q: End of this program
2
input a character
r
ASCII code for character 'r' is -> 114
Input 1,2,3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: Display ASCII TABLE
q: End of this program
q

END

However it runs like so...
$ ./a.exe
ASCII character program

Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>>1
ASCII code -> CHAR
Input ASCII code: 96
Character for ASCII code 96 is -> '`'
Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>> Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>>2
CHAR -> ASCII code
Input a character: ASCII code for character '
' is -> '10'

Input 1, 2, 3, or q
1: ASCII code -> CHAR
2: CHAR -> ASCII code
3: DISPLAY ASCII TABLE
q: End of this Program
>>q

What it is doing wrong is when the user enters 2, to change the CHAR to ASCII code, it won't allow the user to enter anything... The program also displays the main menu twice after each attempt...I can't figure it out....I been working like mad on it for a few days, but it isn't working at all..HELPPP!!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-17-2007, 08:45 AM
v0id's Avatar   
v0id v0id is online now
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,004
Last Blog:
CherryPy(thon)
Rep Power: 21
v0id is just really nicev0id is just really nicev0id is just really nicev0id is just really nice
Send a message via MSN to v0id
Default

You're still missing to flush the standard input stream, before starting the while-loop over again. You have to insert a fflush()-function in the start of the while-loop or at the end of it. If you're looking at the last code I posted in this thread, you can see that there's two fflush()-functions, not only one.

The reason why you have to insert one, is because that after you have selected something in the menu, and again inputs something - f.ex. CHAR => ASCII, then the stdin get new data again - and that data, we've to clear. That's by using the fflush()-function at stdin. If you read my comments in the code, you could see it too.

Code:
#include <stdio.h>

int main()
{
	int  i, type;
	char c;
	type = 0;
	
	printf("ASCII character program\n\n");
	while(type != 'q')
	{
		/* Here's all the other stuff ... */
		

		/* At the end, we need to have this */
		fflush(stdin);
	}
	
	return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I Program another Program? ! bosco General Programming 1 06-15-2007 11:15 AM
! Need urgent help ! Drawing program using cygwin siren C and C++ 0 05-26-2007 10:51 PM
Need help w/ word count program (ASAP) siren C and C++ 1 04-23-2007 08:14 AM
How to modify a program written in .NET 2.0? jackyjack C# Programming 7 03-27-2007 12:26 PM


All times are GMT -5. The time now is 10:28 AM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads