Thread: having trouble using loops

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    8

    having trouble using loops

    Hi guys

    I am trying to write a program that uses a loop to display the characters for ASCII codes 0 through 127. But the catch is 16 characters are to be displayed on each line.

    I have tried working on this program numerous time but haven't gone anywhere yet. any suggestions

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, make a normal loop and check for modulus loop variable 16 is equal to 0, and if so, print a newline.
    In the loop, just print the ASCII character.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Hay thanks for ur help. But can you add more details since i am still a noob in C++ .

    Do i have to use 2 loops.. one loop inside the other ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sample code:
    Code:
    for (int i = 0; i <= 127; i++)
    {
    	if (i &#37; 16 == 0) // This is true only once every 16 loops, when i is dividable by 16 with no rest (ie a whole number, not 2.7897535...)
    	{
    		// Print a newline
    	}
    	// Print your character
    }

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    ok here what i wrote

    Code:
    int main()
    {
    
    char a =0;
    int i;
    
    
    for (int i = 0; i <= 127; i++)
    {
    	if (i &#37; 16 == 0) 
    	{
    		cout<<endl;
    		i=a;
    	}
    	cout<<a;
    }
    
    
    
    
    
    return 0;
    
    
    }
    and i am getting the following error message

    multiple declartion of i in the fuction main.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by wolverine12345 View Post
    ok here what i wrote

    Code:
    int main()
    {
    
    char a =0;
    int i; // Declaration of i here
    
    
    for (int i = 0; i <= 127; i++) // Declaration of i here
    {
    	if (i &#37; 16 == 0) 
    	{
    		cout<<endl;
    		i=a; // What is this supposed to do?
    	}
    	cout<<a;
    }
    
    
    
    
    
    return 0;
    
    
    }
    and i am getting the following error message
    See comments. The error is because you have two i variables. Remove one.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    lol i feel like an idiot now..

    ok i removed the int i; and getting no errors but when i run the program, it hangs.. one of those infinite loops..

    and the reason i am using i=a so a has the same value as i . a is a character variable. so whenever i changes a changes and displays the character

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Erm, you're doing it the wrong way then. You're assigning a to i. Besides, you don't need a. Just print out i.

    Code:
    cout << static_cast<char>(i);
    So simple, yet so powerful.
    And the reason for the infinite loop is because you're assigning a to i (since a is always 0... well, you get my point).
    Last edited by Elysia; 11-28-2007 at 01:44 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [edit] @Elysia: consider recommending C++-style casting (static_cast<>()) rather than C-style casting (()) in this forum, the C++ one. [/edit]

    and the reason i am using i=a so a has the same value as i . a is a character variable. so whenever i changes a changes and displays the character
    Well then you should have it outside the if statement, and it should be a=i, not i=a. You're just setting i to some randome value (because a isn't initialized) with your statement.

    By the way, you can get away with a cast instead of your variable a:
    Code:
    cout << static_cast<char>(i);
    That causes i to be treated as a character, and cout will print a space instead of 32 or whatever.

    It should be noted that you can use the isprint() function from <cctype> to determine whether a character is a normal character that can be printed. You see, many of the characters from 0 to 32 and from 127 to 255 are unprintable, or mess up your screen if you do print them. Backspace, delete, beep, newline, formfeed, carriage return, tab, and more are characters in these ranges that will mess up your display. Consider using isprint().

    You must be using MSVC 6. It's the only compiler that I know of that would complain with this code:
    Code:
    int i;
    for(int i = 0; i < 10; i ++) {}
    That's valid C++, by the way -- although redundant, because the outer i is never used.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    Thank you Elysia and dwks
    The program is running fine now
    I really appreciate your help.

    just one quick last question.
    Is there any quick way to find out the highest and lowest number from a list of numbers. The method that i can think of at the moment is using the if statements . e.g
    if we have 6 numbers a,b,c,d,e,f then
    if a>b>c>d>e>f cout<< a is highest
    if b>a>c>d>e>f cout<< b is highest
    if c>a>b>d>e>f c out << c is highest
    and so on

    but it will take forever to write if we have let's say 1000 numbers..

    any suggestions ?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    [edit] @Elysia: consider recommending C++-style casting (static_cast<>()) rather than C-style casting (()) in this forum, the C++ one. [/edit]
    I forgot I'm used to C-style casting. Bad habit I'm trying to get rid of, but I usually don't do any bad casting so I haven't really run into troubles using C-style casting, pretty much explaining why I haven't used much C++-style casting.

    Quote Originally Posted by wolverine12345 View Post
    Thank you Elysia and dwks
    The program is running fine now
    I really appreciate your help.

    just one quick last question.
    Is there any quick way to find out the highest and lowest number from a list of numbers. The method that i can think of at the moment is using the if statements . e.g
    if we have 6 numbers a,b,c,d,e,f then
    if a>b>c>d>e>f cout<< a is highest
    if b>a>c>d>e>f cout<< b is highest
    if c>a>b>d>e>f c out << c is highest
    and so on

    but it will take forever to write if we have let's say 1000 numbers..

    any suggestions ?
    Believe there are CRT functions for that? Don't know for sure, but I do know there was a thread about it somewhere. Someone else might just help you out there, as well, though, because I don't really know.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's not too difficult if you use arrays. If you use a separate, named variable, then you have to use something like this.
    Code:
    max = MAX(a, MAX(b, MAX(c, d) ) );
    Ouch.

    Consider using arrays. Then it's quite simple. You can use a loop to loop through every element, comparing that element with the maximum value found so far. If this element is larger than the previously known maximum, then you set the maximum to this element.

    Do you know how to use arrays? http://www.cprogramming.com/tutorial/lesson8.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    ok here what i wrote

    Code:
    int main()
    
    {
    const int number = 10;
    int num[number];
    int max=0;
    int min;
    cout<<"Please enter 10 numbers."<<endl;
    cin>>num[0];
    cin>>num[1];
    cin>>num[2];
    cin>>num[3];
    cin>>num[4];
    cin>>num[5];
    cin>>num[6];
    cin>>num[7];
    cin>>num[8];
    cin>>num[9];
    
    
    for (int i= 0; i < 10; i++)
    if (num[i] > max)
    max = num[i];
    cout << "The highest number is " << max<< endl;
    
    
    for (int j= 0; j < 10; j++)
    if (num[i] < min)
    min = num[i];
    cout << "The lowest number is " << min<< endl;
    
    return 0;
    }

    The program seems to find the highest number but for the lowest number it would give me a random value. e.g whenever i enter 1,2,3,4,5,6,7,89,10 . highest number is 10 but for the lowest number it would give me value like 8890.. etc

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by wolverine12345 View Post
    Code:
    for (int j= 0; j < 10; j++)
    if (num[i] < min)
    min = num[i];
    cout << "The lowest number is " << min<< endl;
    'Nuff said
    Btw, that shouldn't even work but due to a compiler bug, quirk or whatever, vars declared inside a for becomes global for the function or block in MSVC6, which goes against the standard.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In case Elysia wasn't being clear: you're using i instead of j.

    Be sure to initialize min as well. And setting max to 0 would backfire if all the numbers were negative. Either set it to INT_MIN (from <climits>) or num[0], once you read in the numbers. (Setting it to num[0] is easier, but doesn't work if there are no elements in the array.)

    Code:
    cin>>num[0];
    cin>>num[1];
    cin>>num[2];
    cin>>num[3];
    cin>>num[4];
    cin>>num[5];
    cin>>num[6];
    cin>>num[7];
    cin>>num[8];
    cin>>num[9];
    Consider using a loop, it would be much simpler.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  4. a little trouble with for loops
    By melee in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 01:46 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM