std::cin issues? What is going on here?? std in vs hard coded value











up vote
0
down vote

favorite












Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17



#include <iostream>
#include <cstring>
using namespace std;

const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}

int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;


}
return r[n];
}

// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}

int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?


for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;

}









share|improve this question
























  • Seems to work fine testing it myself, what about the output is unexpected?
    – Mitchel Paulin
    Nov 10 at 22:16










  • What is the rod cutting problem? Have you tried using a debugger?
    – Kevin
    Nov 10 at 22:16










  • When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the <= which should just be < in the for loop in main().
    – user1118321
    Nov 10 at 22:18






  • 1




    Your program expects 8 numbers but you are giving it only 7.
    – n.m.
    Nov 10 at 22:20










  • Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
    – Johnjames123
    Nov 10 at 22:20















up vote
0
down vote

favorite












Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17



#include <iostream>
#include <cstring>
using namespace std;

const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}

int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;


}
return r[n];
}

// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}

int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?


for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;

}









share|improve this question
























  • Seems to work fine testing it myself, what about the output is unexpected?
    – Mitchel Paulin
    Nov 10 at 22:16










  • What is the rod cutting problem? Have you tried using a debugger?
    – Kevin
    Nov 10 at 22:16










  • When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the <= which should just be < in the for loop in main().
    – user1118321
    Nov 10 at 22:18






  • 1




    Your program expects 8 numbers but you are giving it only 7.
    – n.m.
    Nov 10 at 22:20










  • Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
    – Johnjames123
    Nov 10 at 22:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17



#include <iostream>
#include <cstring>
using namespace std;

const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}

int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;


}
return r[n];
}

// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}

int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?


for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;

}









share|improve this question















Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17



#include <iostream>
#include <cstring>
using namespace std;

const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}

int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;


}
return r[n];
}

// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}

int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?


for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;

}






c++ stdin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:23

























asked Nov 10 at 22:06









Johnjames123

163




163












  • Seems to work fine testing it myself, what about the output is unexpected?
    – Mitchel Paulin
    Nov 10 at 22:16










  • What is the rod cutting problem? Have you tried using a debugger?
    – Kevin
    Nov 10 at 22:16










  • When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the <= which should just be < in the for loop in main().
    – user1118321
    Nov 10 at 22:18






  • 1




    Your program expects 8 numbers but you are giving it only 7.
    – n.m.
    Nov 10 at 22:20










  • Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
    – Johnjames123
    Nov 10 at 22:20


















  • Seems to work fine testing it myself, what about the output is unexpected?
    – Mitchel Paulin
    Nov 10 at 22:16










  • What is the rod cutting problem? Have you tried using a debugger?
    – Kevin
    Nov 10 at 22:16










  • When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the <= which should just be < in the for loop in main().
    – user1118321
    Nov 10 at 22:18






  • 1




    Your program expects 8 numbers but you are giving it only 7.
    – n.m.
    Nov 10 at 22:20










  • Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
    – Johnjames123
    Nov 10 at 22:20
















Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16




Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16












What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16




What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16












When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the <= which should just be < in the for loop in main().
– user1118321
Nov 10 at 22:18




When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the <= which should just be < in the for loop in main().
– user1118321
Nov 10 at 22:18




1




1




Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20




Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20












Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20




Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Comments helped but here was the exact line. that needed changing...
for(int i = 1; i <= n; i++),
i = 1 fixed it in all test cases.
Price list was off .






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',
    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%2f53243891%2fstdcin-issues-what-is-going-on-here-std-in-vs-hard-coded-value%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








    up vote
    0
    down vote













    Comments helped but here was the exact line. that needed changing...
    for(int i = 1; i <= n; i++),
    i = 1 fixed it in all test cases.
    Price list was off .






    share|improve this answer

























      up vote
      0
      down vote













      Comments helped but here was the exact line. that needed changing...
      for(int i = 1; i <= n; i++),
      i = 1 fixed it in all test cases.
      Price list was off .






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Comments helped but here was the exact line. that needed changing...
        for(int i = 1; i <= n; i++),
        i = 1 fixed it in all test cases.
        Price list was off .






        share|improve this answer












        Comments helped but here was the exact line. that needed changing...
        for(int i = 1; i <= n; i++),
        i = 1 fixed it in all test cases.
        Price list was off .







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 22:32









        Johnjames123

        163




        163






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243891%2fstdcin-issues-what-is-going-on-here-std-in-vs-hard-coded-value%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python