Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:05:22+00:00 2026-05-28T18:05:22+00:00

I am very new to programming, so I apologize if this question seems absurdly

  • 0

I am very new to programming, so I apologize if this question seems absurdly simple. I am working on some extra questions in the current chapter of my C++ book. I have actually found a correct answer to the problem, but while doing so, I ran into a situation that is driving me crazy because I can’t figure out WHY one particular solution works and another one doesn’t.

So, the problem asks to print the ASCII values between 32 and 127 in a few rows with 16 characters per row. The solution I have come to (that works correctly) is this:

#include <iostream>
using namespace std;

int main()
{
  char letter;
  int count = 0;

  for (letter = 32; letter < 127; letter++, count++)
    {
      if (count == 16)
        {
          cout << endl;
          count = 0;
        }
      cout << letter << " ";
    }

  cout << endl;

  return 0;

}

Again, the above code runs fine and does what I want it to. The difficulty lies in something I tried before this. I attempted to do the same problem with a nested while loop, like so:

#include <iostream>
using namespace std;

int main()
{
  char letter = 32;
  int count;

  while (letter < 127)
    {
      count = 0;
      while (count < 16)
        {
          cout << letter << " ";
          letter++;
          count++;
        }
      cout << endl;
    }

  cout << endl;

  return 0;

}

This while loop just runs infinitely and also spits out some garbage after the ASCII characters I want, and I can’t figure out why. What’s even weirder is if I change the variable ‘letter’ in the code with while loops to an int instead of a char, it runs exactly the way I want it to and terminates when it should, just displaying the actual numbers instead of the ASCII values.

It’s only when ‘letter’ is a char that I get an infinite loop. I’m sure it’s something really simple, and I might just be too tired to see it right now, but any help/hints would be much appreciated! Even though I technically got the answer, it’s driving me crazy that I don’t know WHY the second answer fails so horribly.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:05:23+00:00Added an answer on May 28, 2026 at 6:05 pm

    The answer is simple, true enough. Here is what happens – (signed)char can have values in the range [-128, 127] in the inner loop after you output the row up to 112, you increment count with another 16 and therefor you also increment letter with 16, this makes letter equal to 112 + 16 = 128, which due to the range of signed char is actually overflowing and becomes -128. So after this execution of the inner loop the condition of the outer loop still holds: -128 < 127. That is also why you get weird chars – these will be the values between -128 and 32.

    How to fix the problem? Change the check in the inner loop:
    while (count < 16 && letter < 127)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to programming, so please forgive me if this question seems
I'm very new to programming so I apologize in advance if my question is
I'm very new to programming and for some reason this program is not calculating
This is a very simple question, and I apologize for being so noobish :/
I am very new at C programming and I am working on a firmware
I am very new at all this c# Windows Phone programming, so this is
I'm somewhat new to OOP programming so it's very likely I'm making some stupid
Firstly, this is a homework assignment, and I am very new to programming in
i'm doing some exercises in my Java book. I'm very new to programming. Therefore,
I very new to programming and I have a simple problem that I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.