Looping Confusion
My code seems to ask twice instead of once if I want to run the code again. I just want it to run normally like my other cases which only ask once after I have viewed the activity and these two activities are the only ones with problems. What seems to be the problem here:
case 5:
cout<<"Here are the list of activities in Activity 5:" << endl;
cout<<"[5.1]Determining a Number within the Array" << endl;
cout<<"[5.2]Determining the Highest and Lowest integer" << endl;
cout<<"[5.3]Reversed Array" << endl;
cin >> choice;
system("CLS");
if(choice == 5.1){
counter +=1;
int nos[10];
int det;
cout <<"Note: Do not input any decimal numbers." << endl;
for(int array = 1; array < 11; array++){
cout << "Input integers 1-10 only. [" << array << "]";
cin >> nos[det];
}
cout << "Type in 1 integer value only.[" << det << "]";
cin >> det;
if(det >= nos[1] || det <= nos[10]){
cout << "The value is within the scope of the array.";
}
else{
cout << "The value is not within the scope of the array.";
}
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
else if(choice == 5.2){
counter +=1;
cout <<"Note: Do not input any decimal numbers." << endl;
cout <<"Enter your integers." << endl;
int nos[10];
int put;
for(int rep = 1; rep < 11; rep++){
cout <<"[" << rep << "]";
cin >> nos[rep];
}
int highnos = nos[1];
int lownos = nos[1];
for(int rep = 1; rep < 11; rep++){
if(nos[rep] > highnos){
highnos = nos[rep];
}
}
cout << "Your highest integer is:" << highnos << endl;
for(int rep = 1; rep > 11; rep++){
if(nos[rep] < lownos){
lownos = nos[rep];
}
}
cout <<"Your lowest integer is:"<< lownos << endl;
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
P.S. my activities also has errors but dont mind it :D
c++ for-loop while-loop switch-statement
add a comment |
My code seems to ask twice instead of once if I want to run the code again. I just want it to run normally like my other cases which only ask once after I have viewed the activity and these two activities are the only ones with problems. What seems to be the problem here:
case 5:
cout<<"Here are the list of activities in Activity 5:" << endl;
cout<<"[5.1]Determining a Number within the Array" << endl;
cout<<"[5.2]Determining the Highest and Lowest integer" << endl;
cout<<"[5.3]Reversed Array" << endl;
cin >> choice;
system("CLS");
if(choice == 5.1){
counter +=1;
int nos[10];
int det;
cout <<"Note: Do not input any decimal numbers." << endl;
for(int array = 1; array < 11; array++){
cout << "Input integers 1-10 only. [" << array << "]";
cin >> nos[det];
}
cout << "Type in 1 integer value only.[" << det << "]";
cin >> det;
if(det >= nos[1] || det <= nos[10]){
cout << "The value is within the scope of the array.";
}
else{
cout << "The value is not within the scope of the array.";
}
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
else if(choice == 5.2){
counter +=1;
cout <<"Note: Do not input any decimal numbers." << endl;
cout <<"Enter your integers." << endl;
int nos[10];
int put;
for(int rep = 1; rep < 11; rep++){
cout <<"[" << rep << "]";
cin >> nos[rep];
}
int highnos = nos[1];
int lownos = nos[1];
for(int rep = 1; rep < 11; rep++){
if(nos[rep] > highnos){
highnos = nos[rep];
}
}
cout << "Your highest integer is:" << highnos << endl;
for(int rep = 1; rep > 11; rep++){
if(nos[rep] < lownos){
lownos = nos[rep];
}
}
cout <<"Your lowest integer is:"<< lownos << endl;
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
P.S. my activities also has errors but dont mind it :D
c++ for-loop while-loop switch-statement
Copy this code into a new project and turn it into a function called by the new project'smain
. That will be much easier for you to experiment on while you hunt the bug AND put you closer to having the Minimal, Complete, and Verifiable example you are required to have when making a post.
– user4581301
Nov 22 '18 at 3:48
1
Array declared asint nos[10]
has ten elements, indexed 0 through 9.nos[10]
exhibits undefined behavior, by way of accessing an index out of bounds. Further, you usenos[det]
beforedet
is initialized - another instance of undefined behavior.
– Igor Tandetnik
Nov 22 '18 at 3:51
add a comment |
My code seems to ask twice instead of once if I want to run the code again. I just want it to run normally like my other cases which only ask once after I have viewed the activity and these two activities are the only ones with problems. What seems to be the problem here:
case 5:
cout<<"Here are the list of activities in Activity 5:" << endl;
cout<<"[5.1]Determining a Number within the Array" << endl;
cout<<"[5.2]Determining the Highest and Lowest integer" << endl;
cout<<"[5.3]Reversed Array" << endl;
cin >> choice;
system("CLS");
if(choice == 5.1){
counter +=1;
int nos[10];
int det;
cout <<"Note: Do not input any decimal numbers." << endl;
for(int array = 1; array < 11; array++){
cout << "Input integers 1-10 only. [" << array << "]";
cin >> nos[det];
}
cout << "Type in 1 integer value only.[" << det << "]";
cin >> det;
if(det >= nos[1] || det <= nos[10]){
cout << "The value is within the scope of the array.";
}
else{
cout << "The value is not within the scope of the array.";
}
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
else if(choice == 5.2){
counter +=1;
cout <<"Note: Do not input any decimal numbers." << endl;
cout <<"Enter your integers." << endl;
int nos[10];
int put;
for(int rep = 1; rep < 11; rep++){
cout <<"[" << rep << "]";
cin >> nos[rep];
}
int highnos = nos[1];
int lownos = nos[1];
for(int rep = 1; rep < 11; rep++){
if(nos[rep] > highnos){
highnos = nos[rep];
}
}
cout << "Your highest integer is:" << highnos << endl;
for(int rep = 1; rep > 11; rep++){
if(nos[rep] < lownos){
lownos = nos[rep];
}
}
cout <<"Your lowest integer is:"<< lownos << endl;
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
P.S. my activities also has errors but dont mind it :D
c++ for-loop while-loop switch-statement
My code seems to ask twice instead of once if I want to run the code again. I just want it to run normally like my other cases which only ask once after I have viewed the activity and these two activities are the only ones with problems. What seems to be the problem here:
case 5:
cout<<"Here are the list of activities in Activity 5:" << endl;
cout<<"[5.1]Determining a Number within the Array" << endl;
cout<<"[5.2]Determining the Highest and Lowest integer" << endl;
cout<<"[5.3]Reversed Array" << endl;
cin >> choice;
system("CLS");
if(choice == 5.1){
counter +=1;
int nos[10];
int det;
cout <<"Note: Do not input any decimal numbers." << endl;
for(int array = 1; array < 11; array++){
cout << "Input integers 1-10 only. [" << array << "]";
cin >> nos[det];
}
cout << "Type in 1 integer value only.[" << det << "]";
cin >> det;
if(det >= nos[1] || det <= nos[10]){
cout << "The value is within the scope of the array.";
}
else{
cout << "The value is not within the scope of the array.";
}
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
else if(choice == 5.2){
counter +=1;
cout <<"Note: Do not input any decimal numbers." << endl;
cout <<"Enter your integers." << endl;
int nos[10];
int put;
for(int rep = 1; rep < 11; rep++){
cout <<"[" << rep << "]";
cin >> nos[rep];
}
int highnos = nos[1];
int lownos = nos[1];
for(int rep = 1; rep < 11; rep++){
if(nos[rep] > highnos){
highnos = nos[rep];
}
}
cout << "Your highest integer is:" << highnos << endl;
for(int rep = 1; rep > 11; rep++){
if(nos[rep] < lownos){
lownos = nos[rep];
}
}
cout <<"Your lowest integer is:"<< lownos << endl;
system ("PAUSE");
system ("CLS");
cout << "Do you want to run the program again? (y/n)" << endl;
cin >> choose;
}
P.S. my activities also has errors but dont mind it :D
c++ for-loop while-loop switch-statement
c++ for-loop while-loop switch-statement
asked Nov 22 '18 at 3:30
Drexmir MentizaDrexmir Mentiza
11
11
Copy this code into a new project and turn it into a function called by the new project'smain
. That will be much easier for you to experiment on while you hunt the bug AND put you closer to having the Minimal, Complete, and Verifiable example you are required to have when making a post.
– user4581301
Nov 22 '18 at 3:48
1
Array declared asint nos[10]
has ten elements, indexed 0 through 9.nos[10]
exhibits undefined behavior, by way of accessing an index out of bounds. Further, you usenos[det]
beforedet
is initialized - another instance of undefined behavior.
– Igor Tandetnik
Nov 22 '18 at 3:51
add a comment |
Copy this code into a new project and turn it into a function called by the new project'smain
. That will be much easier for you to experiment on while you hunt the bug AND put you closer to having the Minimal, Complete, and Verifiable example you are required to have when making a post.
– user4581301
Nov 22 '18 at 3:48
1
Array declared asint nos[10]
has ten elements, indexed 0 through 9.nos[10]
exhibits undefined behavior, by way of accessing an index out of bounds. Further, you usenos[det]
beforedet
is initialized - another instance of undefined behavior.
– Igor Tandetnik
Nov 22 '18 at 3:51
Copy this code into a new project and turn it into a function called by the new project's
main
. That will be much easier for you to experiment on while you hunt the bug AND put you closer to having the Minimal, Complete, and Verifiable example you are required to have when making a post.– user4581301
Nov 22 '18 at 3:48
Copy this code into a new project and turn it into a function called by the new project's
main
. That will be much easier for you to experiment on while you hunt the bug AND put you closer to having the Minimal, Complete, and Verifiable example you are required to have when making a post.– user4581301
Nov 22 '18 at 3:48
1
1
Array declared as
int nos[10]
has ten elements, indexed 0 through 9. nos[10]
exhibits undefined behavior, by way of accessing an index out of bounds. Further, you use nos[det]
before det
is initialized - another instance of undefined behavior.– Igor Tandetnik
Nov 22 '18 at 3:51
Array declared as
int nos[10]
has ten elements, indexed 0 through 9. nos[10]
exhibits undefined behavior, by way of accessing an index out of bounds. Further, you use nos[det]
before det
is initialized - another instance of undefined behavior.– Igor Tandetnik
Nov 22 '18 at 3:51
add a comment |
1 Answer
1
active
oldest
votes
First of all, you haven't initialized anything so everything is taking garbage values.
initialize det first and if you want to input the whole array then run a loop. cin>>nos[det] only inputs one value at a garbage index according to your code.
This code is a mess. Kindly fix initializations and inputs, then share the output you're getting vs the output you want.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423468%2flooping-confusion%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
First of all, you haven't initialized anything so everything is taking garbage values.
initialize det first and if you want to input the whole array then run a loop. cin>>nos[det] only inputs one value at a garbage index according to your code.
This code is a mess. Kindly fix initializations and inputs, then share the output you're getting vs the output you want.
add a comment |
First of all, you haven't initialized anything so everything is taking garbage values.
initialize det first and if you want to input the whole array then run a loop. cin>>nos[det] only inputs one value at a garbage index according to your code.
This code is a mess. Kindly fix initializations and inputs, then share the output you're getting vs the output you want.
add a comment |
First of all, you haven't initialized anything so everything is taking garbage values.
initialize det first and if you want to input the whole array then run a loop. cin>>nos[det] only inputs one value at a garbage index according to your code.
This code is a mess. Kindly fix initializations and inputs, then share the output you're getting vs the output you want.
First of all, you haven't initialized anything so everything is taking garbage values.
initialize det first and if you want to input the whole array then run a loop. cin>>nos[det] only inputs one value at a garbage index according to your code.
This code is a mess. Kindly fix initializations and inputs, then share the output you're getting vs the output you want.
answered Nov 22 '18 at 5:38
Archit JArchit J
607
607
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423468%2flooping-confusion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Copy this code into a new project and turn it into a function called by the new project's
main
. That will be much easier for you to experiment on while you hunt the bug AND put you closer to having the Minimal, Complete, and Verifiable example you are required to have when making a post.– user4581301
Nov 22 '18 at 3:48
1
Array declared as
int nos[10]
has ten elements, indexed 0 through 9.nos[10]
exhibits undefined behavior, by way of accessing an index out of bounds. Further, you usenos[det]
beforedet
is initialized - another instance of undefined behavior.– Igor Tandetnik
Nov 22 '18 at 3:51