Hi there,
quick (and I am sure) simple question on a program I have just created.
here's the code:

#include<iostream>
using namespace std;

int main()
{
char letter;
int counter=0;//set the counter to 0
for (letter=65; letter<=122; letter++)
{if ((((((letter==91)||(letter==92)||(letter==93)||(letter==94)
	 ||(letter==95)||(letter==96))))))continue;
counter++;//increment the counter in the loop
		if (counter==27)//counter condition
		cout<<" \n";
cout<<letter<<" ";

}
counter=0;//reset the counter
return 0;
}

what's bugging me is the counter. I included

int counter=0;

because I wante to have a space between the upper case alphabet and the lower case one, but I had to make it equal to 27 to get the space right and the letters of the alphabet are 26...why is that?
thanks

Recommended Answers

All 5 Replies

Only considering the formatting of the code,
consider a more readable alternative ..

#include<iostream>
using namespace std;

int main()
{
  char letter;
  int counter = 0; //set the counter to 0

  for (letter = 65; letter <= 122; letter++)
  {
    if(   (letter == 91)
      ||  (letter == 92)
      ||  (letter == 93)
      ||  (letter == 94)
      ||  (letter == 95)
      ||  (letter == 96)
    )
      continue;

    counter++; //increment the counter in the loop
    
    if (counter == 27) //counter condition
      cout << " \n";

    cout << letter << " ";
  }

  counter = 0; //reset the counter

  return 0;
}

When the value of the variable letter is 65, the alphabet A is displayed. When the value is 90 the variable Z is displayed. At this time the value of counter is 26. The for the values 91-96 there is no display. Then the value of letter becomes 97. Counter gets incremented to 27, we go to the next line and the letter a is displayed

Hi, yeah, you're quite right about readibility, I didn't know I could break each line like that, I thought it would have caused an error or something.

About this

When the value of the variable letter is 65, the alphabet A is displayed. When the value is 90 the variable Z is displayed. At this time the value of counter is 26. The for the values 91-96 there is no display. Then the value of letter becomes 97. Counter gets incremented to 27, we go to the next line and the letter a is displayed

I think I am following but I thought that if I say

if (counter==26)//counter condition
cout<<" \n";

the line space will be placed after the 26th character but instead it places it after character 25.

I can show you another quick example though, because I don't seem to be getting this counter business and line breaks at all.
This example is in the book I am using for C++(C++ a beginner's guide - Herbert Schildt). it goes:

#include<iostream>
using namespace std;
int main()
{
double f;
double m;
int counter;

counter=0;

for(f=1.0;f<=100.0;f++)
{
m=f/3.28;
cout<<f<<" feet is"<<m<<"meters.\n";
counter++;
if (counter==10)
{
cout<< "\n";
counter=0;
}
}

	return 0;
}

As you can see it's

if (counter==10)
{
cout<< "\n";

so here the line breaks is actually placed after 10 lines as it says in the program, whereas in my program if I say

if (counter==26)//counter condition
cout<<" \n";

the line break goes after the 25th letter...this is what I mean, and I don't understand why to have a line break after the 26th letter I have to write

if (counter==27)//counter condition
cout<<" \n";

and not

if (counter==26)//counter condition
cout<<" \n";

.
thanks

I don't understand why to have a line break after the 26th letter ...

You need to use 27 because you increment the counter before the if-statement.

oh I see,so if I place the increment after the counter I will be able to use 26. I will have a go and post the result, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.