Looping Confusion












-1















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










share|improve this question























  • 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 use nos[det] before det is initialized - another instance of undefined behavior.

    – Igor Tandetnik
    Nov 22 '18 at 3:51


















-1















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










share|improve this question























  • 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 use nos[det] before det is initialized - another instance of undefined behavior.

    – Igor Tandetnik
    Nov 22 '18 at 3:51
















-1












-1








-1








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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'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 use nos[det] before det 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






  • 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



















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














1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























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









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 5:38









        Archit JArchit J

        607




        607
































            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%2f53423468%2flooping-confusion%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

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?