Is it possible to get the length of an array of integers?
Is it possible to get the length of an array of integers?
Not unless it has some kind of end-marker (strings have NULL terminators) which a normal array of integers doesn't have.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
So could i put integers in an string, or a character array? i guess that wouldn't really work...unless i tried to seperate the integers with commas, maybe? i'll see what i can do.
If the size is known at compile time you could do:Code:template<typename T> inline int arraySize(const T* mem) { return (sizeof(mem)/sizeof(T)); }
Well, the size isn't really known at runtime. But for the record, could you please explain how i would use the code you gave me?
Alright, well here is my problem. There is probably a better way to solve this. It's really simple probably, but oh well. I'm not good, that's why i need help.
The boldface text is my problem. I don't want the comma to appear the last occurance of the loop. And for the record, I do have to use the while loop, if it makes a difference.Code:#include <iostream> using std::cout; using std::cin; main(){ int number(0); int devisor(1); int total(0); number=300000; while((number<0) || (number>30000)){ cout<<"Enter a positive integer between 0 and 30000. :"; cin>>number; } cout<<"\nThe factors of "<<number<<" are : "; while(devisor<number){ if(number%devisor==0){ cout<<devisor<<','; } devisor++; } if(total>number){ cout<<"\nThe number entered is abundant.\n"; } else if(total<number){ cout<<"\nThe number entered is deficient.\n"; } else cout<<"\nThe number entered is perfect.\n"; return 0; }
You could delete it. Stick
cout << "\b ";
after the while loop. It's ugly, but would prevent the need for additional conditional testing.
Joe
See, I thought about that. But I decided I would try to do it a different way to get experience, any ideas?
I was thinking about GetCursorPos() but first of all, that takes a pointer to a POINT structure, and I like COORDS better, and also...correct me if i'm wrong, but doesn't it return pixels? I was going to try to SetConsoleCursorPosition() but the conversion from the POINT to COORD didn't really do what i expected it to.
Last edited by Extol; 04-17-2003 at 03:56 PM.
>I was thinking about GetCursorPos() but first of all, that takes a pointer to a POINT structure, and I like COORDS better, and also...correct me if i'm wrong, but doesn't it return pixels? I was going to try to SetConsoleCursorPosition() but the conversion from the POINT to COORD didn't really do what i expected it to.<
From msdn -
If you've got to use a while loop, however, I'm not sure how all the api specific stuff is going to come out in the wash.The cursor position determines where characters written by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsole function, are displayed. To determine the current position of the cursor, use the GetConsoleScreenBufferInfo function.
Joe
That did the trick! Thank you!Code:void setPosition(){ CONSOLE_SCREEN_BUFFER_INFO screenInfo; HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE); COORD cursorLocation={0,0}; GetConsoleScreenBufferInfo(hOutput, &screenInfo); cursorLocation.X=(screenInfo.dwCursorPosition.X)-1; cursorLocation.Y=screenInfo.dwCursorPosition.Y; SetConsoleCursorPosition(hOutput, cursorLocation); }