LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2012, 01:53 PM   #1
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Rep: Reputation: 0
printing a through g using a while loop in c language


Hello, I am trying to print out a list from A to G using the while loop. I am having a hard time doing this. I am going to also give the code that I have created thus far, as well as the output that I have received up until this point. Thank You in advance for any help that you all could offer me.

---------- Post added 04-09-12 at 01:54 PM ----------

Code:


#include <stdio.h>
#define G "G"	// assigns the string value G to the CONSTANT defined as g

int main (void)
{
	char a;
	char n = a;	 // declaration of variable of type char assigned the character value a
	
	while (++a < 'G')
		printf("%5c",a);	// uses character conversion specification of type char and spaces the ouput 5 spaces apart
	printf("\n");
	
	return 0;
}	// end main function

Last edited by UnixCube; 04-09-2012 at 01:55 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-09-2012, 01:59 PM   #2
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
Output given

À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ
1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F
jkc0144@csp09: PuTTY

This is the output that I am getting from the code, it is giving me some odd ASCII values. It does give me the values of A B C D E F, but I'm not sure if this is by default, or if this is my code actually doing this for me.
 
Old 04-09-2012, 01:59 PM   #3
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Let me guess: it's either printing garbage non stop or it falls through straight away?
You need to re-read how to assign a value to a variable and, also, think what a variable is and what it does.
Giving you the answer, I think, would stop you learning -- you should be able to work this out from first principals and a guide to C.
 
Old 04-09-2012, 02:01 PM   #4
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
Similar Program using numbers

Code:
#include <stdio.h>
#define TEN 10	// CONSTANT TEN declaration assigned the value 10
int main(void)
{
	int n = 0;	// declaration of variable n of type int assigned the value 0

	while (n++ < TEN)		// for loop includes subexpression post increment n++ inside of condition
		printf("%5d", n);	// uses the conversion specifier for decimal input and the argument list includes variable n
	printf("\n");			// display newlines when printing output to the screen

	return 0;
}	// end main function
here is the output for this similar program
Code:
  1    2    3    4    5    6    7    8    9   10
 
Old 04-09-2012, 02:04 PM   #5
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
Assigning an initial value to a char variable is a little different to doing it for an integer. Have a look at the material you're learning from and try to work it out.
 
Old 04-09-2012, 02:09 PM   #6
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
273, so variables are read differently when using the char variable instead of the int variable. That makes sense, I'm trying to assign a alphabetical letter that corresponds with a number so then when the while loop reads from 1 through 7 it will correspond with a through g alphabetically.
 
Old 04-09-2012, 02:16 PM   #7
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
You are nearly there but you need to think about what you are assigning as an initial value to n.
You don't need either the define at the top or the variable a.
Think about what each line is doing, especially the lines assigning values to variables. Assigning numerical values to variables is easy as you just use the number but when you want to assign an ASCII character's value to a char you need to do it differently.
 
Old 04-09-2012, 02:21 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
For the example in your first post:

1) You're never using your constant G
2) You're never using your variable n
3) You're never declaring the starting value of your variable a (this is the main reason why your code is not working)

You seem to be getting confused between variables and characters (the actual character, not the variable that contains a character). "char a" is declaring a variable called a, which will contain a character. It's not initialized to anything though, it's just filled with junk. "char n = a" is then declaring a variable n, which will also contain a character. It's being initialized to the contents of the variable a, which is still junk. So now you have two char variables, both of which contain the same junk value. Neither of them are ever set to anything, they're just allocated and filled with junk, and then incremented from there. That's why you're getting a bunch of junk output until it finally happens to reach 'G', which is when it stops like it's supposed to.

Last edited by suicidaleggroll; 04-09-2012 at 02:29 PM.
 
Old 04-09-2012, 02:31 PM   #9
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
suicidaleggrol & 273, thanks a lot for the advice. I'm going to try and read over my text book, on how I can read values and display character types using while loops.
 
Old 04-09-2012, 02:32 PM   #10
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
No problem. As I said I think it's important you read these things for yourself as they're important concepts.
 
Old 04-09-2012, 02:39 PM   #11
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by UnixCube View Post
suicidaleggrol & 273, thanks a lot for the advice. I'm going to try and read over my text book, on how I can read values and display character types using while loops.
Your print is perfectly fine, your first example is literally 4 characters away from working (at least getting to the point where you can tweak it from there). All you need to do is give your variable "a" a starting value, just like where you said "int n = 0" in your second example.
 
Old 04-09-2012, 02:51 PM   #12
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
I'm making some progress here, I have gotten rid of the #define statement as was suggested. I then took out the char = n; assignment declaration.

Code:
/*a_g page 166 #8 chapter 5 program that prints out the alphabetical letters from a to g*/

#include <stdio.h>

int main (void)
{
	char ch;	// declaration of variable of type char assigned the character value a
	
	
	int a = 65; 	// declaration of variable a given the value 1
	
	while ( ++a <= 73  )	// while condition
		{
		printf("%5c",ch);	// uses character conversion specification of type char and spaces the ouput 5 spaces apart
		printf("\n");
		ch++;
		}	// end while block
	return 0;
}	// end main function
here is the output code for this program

Code:
    @
    A
    B
    C
    D
    E
    F
    G
I'm not sure where the @ symbol is coming from though. But besides that, this is exactly what I wanted to output.
 
Old 04-09-2012, 02:57 PM   #13
273
LQ Addict
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
I don't know why that is working because I think you made a mistake not giving ch an initial value.
You can make a a charand use it rather than ch in your loop to output the character and you can initialise a by assigning it the value of character 'A'.
 
2 members found this post helpful.
Old 04-09-2012, 03:05 PM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by UnixCube View Post
I'm making some progress here, I have gotten rid of the #define statement as was suggested. I then took out the char = n; assignment declaration.

Code:
/*a_g page 166 #8 chapter 5 program that prints out the alphabetical letters from a to g*/

#include <stdio.h>

int main (void)
{
	char ch;	// declaration of variable of type char assigned the character value a
	
	
	int a = 65; 	// declaration of variable a given the value 1
	
	while ( ++a <= 73  )	// while condition
		{
		printf("%5c",ch);	// uses character conversion specification of type char and spaces the ouput 5 spaces apart
		printf("\n");
		ch++;
		}	// end while block
	return 0;
}	// end main function
here is the output code for this program

Code:
    @
    A
    B
    C
    D
    E
    F
    G
I'm not sure where the @ symbol is coming from though. But besides that, this is exactly what I wanted to output.
You're still not giving your character an initial value, it's just filled with trash. Then you're printing it and incrementing it as if you know what it is, but it's just filled with trash. All you've done is switch your loop to be over an integer rather than the character itself, but this still suffers from all of the problems the first example did.

Go back to your first example, and change your "char a" to "char a = 'A'"

You NEVER want to use a variable before you initialize it to something.

Last edited by suicidaleggroll; 04-09-2012 at 03:07 PM.
 
1 members found this post helpful.
Old 04-09-2012, 03:07 PM   #15
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
273, I initialized my char variable ch to the string character 'A',
then when I ran the program it gave me perfect output.


Code:
/*a_g page 166 #8 chapter 5 program that prints out the alphabetical letters from a to g*/

#include <stdio.h>

int main (void)
{
	char ch = 'A';	// declaration of variable of type char assigned the character value a
	
	
	int a = 65; 	// declaration of variable a given the value 1
	
	while ( ++a <= 72  )	// while condition
		{
		printf("%5c",ch);	// uses character conversion specification of type char and spaces the ouput 5 spaces apart
		printf("\n");
		ch++;	// increments the character values and returns them in their ASCII character form
		}	// end while block
	return 0;
}	// end main function
OUTPUT:
Code:
    A
    B
    C
    D
    E
    F
    G
By initializing the char variable ch I was able to get rid of that pesky @ symbol. Thanks again 273.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
C Language - For Loop that just keeps on runnin' Completely Clueless Programming 58 08-02-2010 11:29 AM
[SOLVED] Assembly language printing integers to the stdout using int 0x80 yaami Programming 9 07-08-2010 10:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration