LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 03:10 PM   #16
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0

@suicidaleggroll, thank you.

I just initialized the character variable ch to the string value 'A' and I think that is what fixed my error. Before that initialization you were right, the value that I had initially stored was all nonsense. Thank you again.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-09-2012, 03:14 PM   #17
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
What you have there will work. It's a little overly complex for my taste (for example, why is the variable a running from 65 to 72 instead of 0 to 7? why are you using ++a instead of a++? just little nitpicky things like that), but it will work consistently.
 
Old 04-09-2012, 03:16 PM   #18
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
Thanks suicidaleggroll, I fixed the problem by initializing the char ch variable to 'A'. That helped out a lot.
 
Old 04-09-2012, 03:22 PM   #19
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
I know you already solved this, but sometimes it helps to see other solutions to the same problem. Here is what I would probably do for this:
Code:
#include <stdio.h>

int main (void)
{
        char a;
        for (a='A';a<='G';a++) printf("%5c",a);
        printf("\n");
        return 0;
}
End result is the same, but it's a little easier to follow, and would probably execute a bit faster since you only have one variable being incremented every iteration of the loop instead of two. Just another way of approaching the problem.
 
2 members found this post helpful.
Old 04-09-2012, 03:25 PM   #20
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
Quote:
Originally Posted by suicidaleggroll View Post
You NEVER want to use a variable before you initialize it to something.
Quoted in agreement.
 
Old 04-09-2012, 03:27 PM   #21
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
@suicidaleggroll
I think that it could work that way using the for loop as well. You are right. I only chose the while loop for the practice. Also, I tried changing the while condition using the range from 0 to 7, and that did not work. I have to keep the condition from ++ch < = 72, or it will give me an infinite loop.
 
Old 04-09-2012, 03:41 PM   #22
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You know you can compare against character literals too, right? The value 72 is not particularly useful (unless you can remember the ASCII values). I find it more readable to use the character you're interested in comparing with (as suicidaleggroll did above). With a while loop, this is (C++; obviously you can use printf() rather than cout), e.g.

Code:
char ch = 'A';
  
while(ch <= 'G')
{
  cout << ch << endl;
  ch++;
}
 
Old 04-09-2012, 03:48 PM   #23
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
Also, I tried changing the while condition using the range from 0 to 7, and that did not work. I have to keep the condition from ++ch < = 72, or it will give me an infinite loop.
You must not have done it correctly.

Code:
int a = 65;
while ( ++a <= 72  )
{
   do stuff with ch
}
is exactly the same as
Code:
int a = 0;
while ( ++a <= 7  )
{
   do stuff with ch
}

Last edited by suicidaleggroll; 04-09-2012 at 03:49 PM.
 
Old 04-09-2012, 03:52 PM   #24
UnixCube
Member
 
Registered: Feb 2012
Location: Texas
Posts: 58

Original Poster
Rep: Reputation: 0
suicidaleggroll

I am going to try some more with the code, that you are talking about. Using different conditions for the while loop. I think I did it wrong when I put the values in myself, I will try again.
 
  


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 11:44 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