c++ - Abnormal Output from program -
i have program should print out 2 lists 1 bellow, same list backwards, works first time prints weird output show bellow well.
0123456789 9876543210
however actual output program this:
can tell me wrong code please, not sure why getting output.
void createfile(){ ofstream myfile; myfile.open ("test1.txt"); for(int x = 0; x < 10; x++){ myfile << x << "\n"; } myfile.close(); } void poparray(int array1[]){ ifstream infile("test1.txt"); int x; while(infile >> array1[x]){ cout << array1[x]; } } void reverselist(int array2[]){ for(int x = 9; x > -1; x--){ cout << setw(2) << array2[x]; } } void checklists(int array1[], int array2[], int sizeofarray){ for(int x = array1[1]; x < sizeofarray; x++){ for(int y = array2[1]; x < sizeofarray; x++){ if(x == y) cout << "palindrome" << endl; else{ cout << "not" << endl; } } } } int main() { int array1[10]; int array2[10]; createfile(); poparray(array1); cout << "\n"; reverselist(array1); cout << "\n"; checklists(array1, array2, 10); }
this problem think:
void poparray(int array1[]){ ifstream infile("test1.txt"); int x; while(infile >> array1[x]){ cout << array1[x]; } }
you never specify x is. if understand you're trying think should work:
void poparray(int array1[]){ ifstream infile("test1.txt"); int x; (x=0; x < 10; x++){ cout << array1[x]; } }
just read through bit more. you've got errors here:
void checklists(int array1[], int array2[], int sizeofarray){ for(int x = array1[1]; x < sizeofarray; x++){ for(int y = array2[1]; x < sizeofarray; x++){ if(x == y) cout << "palindrome" << endl; else{ cout << "not" << endl; } } } }
i'd like:
bool checklists(int array1[], int array2[], int sizeofarray){ bool ispalindrome=true; for(int x = 0; x < sizeofarray; x++){ if(array1[x] != array2[sizeofarray-(x+1)]){ ispalindrome = false; } } return ispalindrome; }
then @ end of main
have:
if(checklists(array1, array2, 10)){ cout << "is palindrome\n"; } else{ cout << "is not palindrome\n"; }
while i'm @ might fix too:
void reverselist(int array2[]){ for(int x = 9; x > -1; x--){ cout << setw(2) << array2[x]; } }
change to:
void reverselist(int array1[], int array2[]){ for(int = 0; < 10; i++){ array2[9-i] = array1[i]; } }
i think if put bits of answer should more or less have works. i've not tested myself though.
Comments
Post a Comment