We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,001 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Problem with For

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    char stupac1, stupac2;
    for (static_cast<char>(stupac1)= 'A'; stupac1 <= 'Z'; ++stupac1) {
        for (static_cast<char>(stupac2) = 'a'; stupac2 <= 'z'; ++stupac2)
            cout << stupac1 << setw(5) <<stupac2 << endl;
    }
    return 0;
}

So before i make mistakes explaining this, anyone who wants to help should run this first.
The code works more or less and since im a beginner(new to for aswell) and i wrote this without making a single error, i feel proud.

The problem:
The stupac1 from A-Z repeats the same letters too many times and I don't know how to make it work (note this is from a task in a book, and the code itself should not be changed (for must be used)).

Thanks in advance.

2
Contributors
3
Replies
2 Hours
Discussion Span
10 Months Ago
Last Updated
4
Views
Jackk123
Deleted Member

lines 8 and 9: remove the typecast because it isn't necessary. If all you want to do is display all the alpha letters A-Z and a-z then the two loops should not be nested. What does your book say how the letters should be displayed?

Ancient Dragon
Achieved Level 70
Team Colleague
32,128 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69

It says that i should write a program with 2 consecutive loops that will make a table ...... which has the letters A-Z and a-z in 2 colums
The task itself told me to use static_cast<char> (kind of a hint/help to make it easier on me)

After which im supposed to rewrite the code so that it has 1 loop in 4 colums.

Jackk123
Deleted Member

Update on the code (still same result)

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    char stupac1, stupac2;
    for (stupac1 = 'A'; stupac1 <= 'Z'; ++stupac1)
    for (stupac2 = 'a'; stupac2 <= 'z'; ++stupac2)
    cout << stupac1 << setw(5) <<stupac2 << endl;
    return 0;
}
Jackk123
Deleted Member

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0747 seconds using 2.71MB