0

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;
}
Ylli F
  • 17
  • 5
  • Welcome to Programmers. This is off-topic here and belongs on StackOverflow. Please don't re-ask it there as this can be migrated. A good rule to follow is if your question has you in front of your IDE it belongs on SO. If it has in front of a whiteboard it belongs on Programmers. Please take a moment to read the site's [FAQ] to learn more about asking question here. – Walter Jan 05 '13 at 21:37
  • also WTH are you using goto? learn about loops and try not to use gotos they are almost **never** needed – ratchet freak Jan 05 '13 at 21:42
  • 2
    Those `goto` statements are realllly unnecessary. – chris Jan 05 '13 at 21:47
  • @ratchetfreak: lol, I know about loops, but this is what the assignment said to use! – Ylli F Jan 05 '13 at 21:47
  • 5
    Wait, you *have* to use goto instead of a proper loop? That's absurd! – chris Jan 05 '13 at 21:48
  • 2
    Seriously, if they *make* you write code like this, get a new instructor/school/book/etc. – NPE Jan 05 '13 at 21:52
  • @NPE, New school? Talk about ragequitting! – chris Jan 05 '13 at 21:52
  • @chris: If that's the level of tuition, this may be the only way forward! :) (nice word, BTW) – NPE Jan 05 '13 at 21:54
  • 2
    If I had to use `goto`, I would name my label `http://stackoverflow.com/questions/46586/goto-still-considered-harmful`. – chris Jan 05 '13 at 21:54
  • @All: Thanks; Just started C++ class from the ground(zero), we came down here so far, so he gave us this assignment "Print all uppercase letters using if(goto)" :( – Ylli F Jan 05 '13 at 22:00
  • @chris: good one, it would even compile. in your `goto` statement you would have to replace `:` with `;`, but i guess it would not be that cumbersome if using `goto` is considered acceptable at all – Andy Prowl Jan 05 '13 at 22:00
  • @AndyProwl, I have some "Cool features of C++" question on SO to thank for that, but I'm having trouble finding it! – chris Jan 05 '13 at 22:01
  • Note that you'll never print the uppercase "Z". – Hot Licks Jan 05 '13 at 22:02
  • @chris: if you do, please share! – Andy Prowl Jan 05 '13 at 22:02
  • @AndyProwl, There it was, a side link off of one I found on Google: http://stackoverflow.com/questions/75538/hidden-features-of-c?lq=1 – chris Jan 05 '13 at 22:04
  • @chris: analog literals made my day. thank you! – Andy Prowl Jan 05 '13 at 22:06

4 Answers4

3

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;
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
2

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

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;
}
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
1

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;

exexzian
  • 7,782
  • 6
  • 41
  • 52
  • the changes you mention are not enough, as it would never end up increasing `character`. it is also needed to add a `character++` after the `cout` statement. – Andy Prowl Jan 05 '13 at 22:10