Ylli F
Ylli F

Reputation: 17

Printing uppercase letters using 'if'

What did I do wrong? I need this, to print all uppercase letters using 'if', but something is wrong because it is printing a letter more than once! Any help appreciated :)

//Uppercase letters using if;
#include<iostream>
using namespace std;

int main()
{
    char character='A';
label1:
if(character>='A')
    if(character>='Z')
        goto label2;
    else
    {
        cout<<character<<endl<<character++<<endl;
        goto label1;
    }

label2:
    cout<<"End"<<endl;
    return 0;
}

Upvotes: 0

Views: 1745

Answers (4)

Andrew Morton
Andrew Morton

Reputation: 25067

A bit late, but:

#include "stdafx.h"
#include<iostream>
#include <string>

using namespace std;

int main()
{
    string s("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    if (1) goto label1;
    cout << "End" << endl;
    return 0;
label1:
    cout << s << endl;
    return 0;
}

Upvotes: 2

exexzian
exexzian

Reputation: 7890

Your code missed two points -

  1. it was not printing Z
  2. printing character two times cout<<character<<endl<<character++<<endl;

change: if(character>='Z') to -> if(character>'Z')

EDITED character++ ( that was typo) cout<<character<<endl<<character++<<endl; to -> cout<<character++<<endl;

Upvotes: 1

Andy Prowl
Andy Prowl

Reputation: 126552

You increase the character variable twice. I am posting this answer just to show you what is the technical mistake, but there is a much bigger conceptual one, which is the use of goto. I suggest you to read some introductory book on C++.

//Uppercase letters using if;
#include<iostream>
using namespace std;

int main()
{
    char character='A';

label1:
if(character>='A')
    if(character>'Z'))
        goto label2;
    else
    {
        cout<<character<<endl;
        character++;
        goto label1;
    }

label2:
    cout<<"End"<<endl;
    return 0;
}

Upvotes: 3

NPE
NPE

Reputation: 500883

The reason it's printing each letter more than once is the following:

    cout<<character<<endl<<character++<<endl;
          ^^^^^^^^^        ^^^^^^^^^

Each of the above would result in character getting written to cout.

Upvotes: 2

Related Questions