-3

The question asks: Rewrite the character value example from the previous Try this to use a for-statement.

With that being said, what I wrote for the previous question was the following which runs fine as well:

#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "cmath"

using namespace std;

int main()
{
    char letter = 'a';
    int i = 97;

    while (i < 123) {
        cout << letter << '\t' << i << '\n';
        i = i + 1;
        letter = letter + 1;

      }
}

EDIT: After thinking about it overnight I made some changes:

int main()
{
    char letter = 'a';

    for (int i = 97; i < 123; i = i + 1)
        cout << letter << '\t' << i << '\n';
        letter = letter + 1;
}

So this is still wrong, but I have gotten closer. I am having difficulty having the control variable on my char type count and line up with the int variable counter. What is it that I am doing wrong?

Darkproduct
  • 1,062
  • 13
  • 28
D.C. the III
  • 340
  • 3
  • 14
  • 1
    Consider adopting the [Allman Indentation Style](https://en.wikipedia.org/wiki/Indentation_style#Allman_style) it is designed to make errors with scope and bracketting really stand out. – user4581301 Nov 24 '18 at 04:59
  • 1
    @eukaryota edited the code to what I had written in my compiler. I get an error underline on the third semicolon after **i = i+1;** and as a result my variable "letter" is not recognized. – D.C. the III Nov 24 '18 at 05:03
  • 2
    @dc3rd turn on warnings when you compile - all of them. – Ted Lyngmo Nov 24 '18 at 05:04
  • 1
    @dc3rd Your edit messed up the includes a bit and I think `pch.h` actually is not supposed to be in brackets (it is non-standard so I am not sure). The error message is telling you that there is not supposed to be another semicolon after the first two between `for (` and `)`. Look again at the explanation of `for` and what all the parts between the semicolons do. What do you expect the individual parts of your current code to do? –  Nov 24 '18 at 05:06
  • @eukaryota I did an edit to my original post. After looking over it and going over each step in my code, I am still having an issue trying to get the **char** variable to behave correctly. I would like the **char** variable to work in tandem with the **int** variable but haven't been able to get that to work. – D.C. the III Nov 24 '18 at 17:53
  • @dc3rd `cmath` is standard by the way and should definitely be in `<>`. Also see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice about why you should not use `using namespace std;`. –  Nov 24 '18 at 20:11

1 Answers1

3

So what you are missing now is only the braces:

int main()
{
    char letter = 'a';

    for (int i = 97; i < 123; i = i + 1)
    {
        cout << letter << '\t' << i << '\n';
        letter = letter + 1;
    }
}

All of these control structures expect a block of statements after it (e.g. if, else, for, while). A block of statements is enclosed in braces. However all of these controls also accept a single statement instead of a block of statements. So if there is no opening brace following the for(...) then only the next statement will be considered part of the loop iteration. But in your case you want both following statements to be executed in each loop iteration, so the braces are needed. It is good practice to always use the braces, even if only one statement is part of the loop body.

From your earlier version it seemed that you want to include both the i, as well as the letter as loop variables in your for loop. This is partially possible, but the individual statements must be in the matching section between the ;, e.g.:

int main()
{
    char letter = 'a';

    for (int i = 97; i < 123; i = i + 1, letter = letter + 1)
    {
        cout << letter << '\t' << i << '\n';
    }
}

or

int main()
{
    char letter;
    int i;

    for (i = 97, letter='a'; i < 123; i = i + 1, letter = letter + 1)
    {
        cout << letter << '\t' << i << '\n';
    }
}

However you cannot declare two variables of different type in one for statement.

Also besides the problem of the for loop: I don't have a reference for the exercise at hand, but is it really required to iterate both i and letter? It seems that you hold the ASCII integer representation of letter in i, so you can just iterate either i or letter, remove the other one, and cast appropriately in your output:

int main()
{
    for (int i = 97; i < 123; i = i + 1)
    {
        cout << static_cast<char>(i) << '\t' << i << '\n';
    }
}

(Although it should be noted that this makes the assumption that the character set encoding is (a super-set of) ASCII, but I think in practice this is almost always true)

  • Damn right before you wrote this I had just figured it out too. I will never forget the use of curly braces to contain the scope of your statements again. Thank you very much for your well thought out answer. I most likely do not require to iterate over both variables, but I am as yet familiar with **static_cast(i)** function. In due time I will get there. – D.C. the III Nov 24 '18 at 20:11