Thread: strlen...int?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    strlen...int?

    Is it possible to get the length of an array of integers?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    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.

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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)); }

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    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?

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Here is my problem

    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.

    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;
    }
    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.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    You could delete it. Stick

    cout << "\b ";

    after the while loop. It's ugly, but would prevent the need for additional conditional testing.
    Joe

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    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.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >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 -

    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.
    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.
    Joe

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    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);
    }
    That did the trick! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM