[SOLVED] printing a through g using a while loop in c language
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ
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.
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
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.
#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
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
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.
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.
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
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.
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.
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.
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.
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.
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
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'.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.