Replace while loop with a for loop












-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?










share|improve this question




















  • 1





    Consider adopting the Allman Indentation Style it is designed to make errors with scope and bracketting really stand out.

    – user4581301
    Nov 24 '18 at 4: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.

    – dc3rd
    Nov 24 '18 at 5:03






  • 2





    @dc3rd turn on warnings when you compile - all of them.

    – Ted Lyngmo
    Nov 24 '18 at 5: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?

    – user10605163
    Nov 24 '18 at 5: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.

    – dc3rd
    Nov 24 '18 at 17:53
















-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?










share|improve this question




















  • 1





    Consider adopting the Allman Indentation Style it is designed to make errors with scope and bracketting really stand out.

    – user4581301
    Nov 24 '18 at 4: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.

    – dc3rd
    Nov 24 '18 at 5:03






  • 2





    @dc3rd turn on warnings when you compile - all of them.

    – Ted Lyngmo
    Nov 24 '18 at 5: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?

    – user10605163
    Nov 24 '18 at 5: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.

    – dc3rd
    Nov 24 '18 at 17:53














-3












-3








-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?










share|improve this question
















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?







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 12:07









Darkproduct

18915




18915










asked Nov 24 '18 at 4:41









dc3rddc3rd

1097




1097








  • 1





    Consider adopting the Allman Indentation Style it is designed to make errors with scope and bracketting really stand out.

    – user4581301
    Nov 24 '18 at 4: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.

    – dc3rd
    Nov 24 '18 at 5:03






  • 2





    @dc3rd turn on warnings when you compile - all of them.

    – Ted Lyngmo
    Nov 24 '18 at 5: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?

    – user10605163
    Nov 24 '18 at 5: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.

    – dc3rd
    Nov 24 '18 at 17:53














  • 1





    Consider adopting the Allman Indentation Style it is designed to make errors with scope and bracketting really stand out.

    – user4581301
    Nov 24 '18 at 4: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.

    – dc3rd
    Nov 24 '18 at 5:03






  • 2





    @dc3rd turn on warnings when you compile - all of them.

    – Ted Lyngmo
    Nov 24 '18 at 5: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?

    – user10605163
    Nov 24 '18 at 5: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.

    – dc3rd
    Nov 24 '18 at 17:53








1




1





Consider adopting the Allman Indentation Style it is designed to make errors with scope and bracketting really stand out.

– user4581301
Nov 24 '18 at 4:59





Consider adopting the Allman Indentation Style it is designed to make errors with scope and bracketting really stand out.

– user4581301
Nov 24 '18 at 4:59




1




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.

– dc3rd
Nov 24 '18 at 5:03





@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.

– dc3rd
Nov 24 '18 at 5:03




2




2





@dc3rd turn on warnings when you compile - all of them.

– Ted Lyngmo
Nov 24 '18 at 5:04





@dc3rd turn on warnings when you compile - all of them.

– Ted Lyngmo
Nov 24 '18 at 5:04




1




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?

– user10605163
Nov 24 '18 at 5:06





@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?

– user10605163
Nov 24 '18 at 5: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.

– dc3rd
Nov 24 '18 at 17:53





@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.

– dc3rd
Nov 24 '18 at 17:53












1 Answer
1






active

oldest

votes


















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)






share|improve this answer
























  • 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<char>(i) function. In due time I will get there.

    – dc3rd
    Nov 24 '18 at 20:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455191%2freplace-while-loop-with-a-for-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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)






share|improve this answer
























  • 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<char>(i) function. In due time I will get there.

    – dc3rd
    Nov 24 '18 at 20:11
















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)






share|improve this answer
























  • 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<char>(i) function. In due time I will get there.

    – dc3rd
    Nov 24 '18 at 20:11














3












3








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)






share|improve this answer













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)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 20:00









user10605163user10605163

2,848624




2,848624













  • 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<char>(i) function. In due time I will get there.

    – dc3rd
    Nov 24 '18 at 20:11



















  • 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<char>(i) function. In due time I will get there.

    – dc3rd
    Nov 24 '18 at 20:11

















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<char>(i) function. In due time I will get there.

– dc3rd
Nov 24 '18 at 20:11





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<char>(i) function. In due time I will get there.

– dc3rd
Nov 24 '18 at 20:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455191%2freplace-while-loop-with-a-for-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

Berounka

I want to find a topological embedding $f : X rightarrow Y$ and $g: Y rightarrow X$, yet $X$ is not...