Welcome! Log In Create A New Profile

Advanced

Ass2Q7

Posted by united 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Ass2Q7
May 07, 2007 08:50PM
For question 7 using stack for infix and postfix expression. Do we pass the infix expression as a string? Any tips on this question especially about designing a class for the expressions will be much appreciated? Thanks and goodluck to you all for this assignment.
Re: Ass2Q7
May 14, 2007 02:45PM
I would say YES, at least that's what I did.
Re: Ass2Q7
June 04, 2007 08:50AM
This brings up an interesting question!

I would also agree to passing it as a string, but instead of passing it as a pure string I used a character-array, mainly because when using a string you'll probably also use cin>> but that would prevent the user from entering any space characters in the infix expression. So I used a character-array and used "gets()" to get the input.

The question is: is there any way to make cin>> terminate the copying on an eol character like a line-feed, instead of a space?
Re: Ass2Q7
June 04, 2007 09:22AM
If I understand your question:

getline(cin, myString);

Try that out and let us know.

Best wishes,
Richard
Re: Ass2Q7
June 04, 2007 10:05AM
#include <iostream>
#include <string>
using namespace std;

int main() {
string myString;
cout << "Enter a string: ";
getline(cin, myString);
cout << "String is: " << myString;
return 0;
}

WORKS PERFECTLY!!! darn, I should have asked the question much much earlier... THANKS A LOT
Re: Ass2Q7
June 04, 2007 10:07AM
PS: from now on I'll be using getline() rather than gets(), because I've been warned against using the latter due to security reasons.

WHAT SECURITY REASONS ARE THESE???
Re: Ass2Q7
June 04, 2007 01:32PM
I had not heard of the security problems you mentioned.

Glad the getline code was useful.

Best wishes,
Richard
Re: Ass2Q7
June 04, 2007 04:57PM
I also used getline, but you have to be carefull when reading more than 1 line.
Re: Ass2Q7
June 06, 2007 08:19AM
Plz explain?
avatar Re: Ass2Q7
June 19, 2007 12:40PM
The function call gets() is defined as such.

char *gets(char *s);

You "give" gets a pointer to an array; and gets fills it up with data.

Now a pointer to a character array is being passed.

However what happens when the user input can exceed the actual allocated space in that array?

For example if you array is 5 characters and the user types in 10 characters what happens?

Well, what you would hope would happen does not.

The memory is just treated as if were actually holding a bigger array.

[0][1][2][3][4][x][q][t][y][z]

And would put characters into x,q etc etc.

This is a security hole and leads to danger... if someone can work out how to put some code into the memory and then run it....

Rather use
char *fgets(char *s, int size, FILE *stream);
Re: Ass2Q7
June 19, 2007 05:25PM
Thanks Ilan,
Sorry for not explaining my previous comment re getline yet.
I will do so shortly, just need to take another look at it.
Sorry, only registered users may post in this forum.

Click here to login