Welcome! Log In Create A New Profile

Advanced

June 2009 exam paper...Question 2.

Posted by tshax 
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
June 2009 exam paper...Question 2.
November 11, 2009 09:11AM
Hi All,

I am having a problem when compiling question2 . My code is:

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
2.1 string stringABC[100];
char stringCheck[100];
2.2 vector<int> vectorEncrypted;

cout<<"Enter a one word string that should be decoded:"<<endl;

for (int i=0; i<strlen(stringCheck);i++)
{
2.3 cin>>stringCheck;
int j = 0;
bool notFound = true;
while ((stringABC[j] != '\0'winking smiley && notFound)
{
if (stringCheck == stringABC[j])
{

notFound = false;
2.4 vectorEncrypted.push_back(strlen(stringCheck));
}
else
j++;

}

2.5 cout<<endl<<"The encrypted string is "<<vectorEncrypted.size();
cout<<endl;
}


return 0;
}

The program gives a me a following message:
no match for 'operator!=' in 'stringABC[j] != '\000''
no match for 'operator==' in 'stringCheck == stringABC[j]'

Please can anyone advise how to tackle this question, Thank you..
avatar Re: June 2009 exam paper...Question 2.
November 11, 2009 02:06PM
You are not comparing the correct things.

On your line 2.1, you're defining stringABC to be an array of string objects. In the expression stringABC[j] != '\0', you're selecting the string from element j of the array and then comparing it to a char. You can't compare a string to a char.

A similar thing is happening for the other error. You're comparing a char array with a string. They're not compatable. With c-style strings, you should be using the strcmp function to compare the contents of the array otherwise you're just comparing the array's address with something else.
Re: June 2009 exam paper...Question 2.
November 11, 2009 04:09PM
I get your point...below is an amended code, but still not working..

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
string stringABC[]={"ABCDEFGHIKLMNOPQRSTUWVXYZ"};
char stringCheck[26];
vector<int> vectorEncrypted;

cout<<"Enter a one word string that should be decoded:"<<endl;
cin>>stringCheck;
for (int i=0; i<strlen(stringCheck);i++)
{

int j = 0;
bool notFound = true;
while ((stringABC[j] != '\0'winking smiley && notFound)
{
if (stringCheck == stringABC[j])
{

notFound = false;
stringCheck = j;
vectorEncrypted.push_back(j);
}
else
j++;

}

}

for (int i=0; i<strlen(stringCheck);i++)
{
cout<<endl<<"The encrypted string is "<<i;
cout<<endl;confused smiley
}

return 0;
}
avatar Re: June 2009 exam paper...Question 2.
November 11, 2009 08:34PM
I don't think you got my point at all. You've still got the same problem you had in the first example, just with different sized arrays.

Let me show you what's in memory:

stringABC[0] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[1] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[2] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[3] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[4] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[5] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[6] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[7] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
stringABC[8] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
...
stringABC[25] : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

If the user enters "Hello World!" at the prompt, stringCheck will be in memory as:

stringCheck[0] : 'H'
stringCheck[1] : 'e'
stringCheck[2] : 'l'
stringCheck[3] : 'l'
stringCheck[4] : 'l'
stringCheck[5] : '0'
stringCheck[6] : ' '
stringCheck[7] : 'W'
stringCheck[8] : 'o'
stringCheck[9] : 'r'
stringCheck[10] : 'l'
stringCheck[11] : 'd'
stringCheck[12] : '!'
stringCheck[13] : '\0'
...
the rest of the array will have random values.

Now when you execute (stringABC[j] != '\0'winking smiley you are actually retrieving an entire string object. Since all your elements were initialised to the same value, it will return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

You may now see that trying to compare a string to a char will force the system to try and find a function defined somewhere:

bool operator !=(const string &left, const char &right);

This operator is not defined by default and hence your error message.

Next, stringCheck is an array of char elements. The array itself is a pointer to char values, ie char *. When trying to execute (stringCheck == stringABC[j]), once again, you get a string back from stringABC[j] and the system tries to find the following function

bool operator == (const char *left, const string &right)

Once again, this is not there by default.

===============================================================

I'm not doing this course so I don't have the exam question. I don't know what you are trying to achieve but I will speculate that what you actually want is to compare individual characters to find out which letters they represent.

In this case, you will can dispence with the whole char [] thing altogether, just use strings for both charCheck and for stringABC

string stringABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string charCheck;

......

cin >> charCheck; // this will still work the same way you wanted it before

....

(stringABC[j] != '\0'winking smiley will now work because you now have a single string and not an array of strings. The string class has an operator override member for [] that will retrieve the nth character from the string, which is what I think you want. In this case stringABC[j] will return a character this time and not a string object. Characters can be compared to characters, so this error should disappear.

(checkChar == stringABC[j]) will not work though. I'm not sure what you want to accomplish but there's a hidden bug that you might want to look at:

stringCheck = j; // this will cause an error because you can't assign an integer to a string (you may get away with assigning it to a char array but this is dangerous).
Re: June 2009 exam paper...Question 2.
November 12, 2009 10:46AM
I got it now, what am trying to achieve is that a program should accept a character and output in which position(number) is that character in an array. The code below do exactly that: Thanksdrinking smiley

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
char stringABC[]={"ABCDEFGHIKLMNOPQRSTUWVXYZ"};
char stringCheck[26];
vector<int> vectorEncrypted;

cout<<"Enter a one word string that should be decoded:"<<endl;
cin>>stringCheck;
for (int i=0; i<strlen(stringCheck);i++)
{

int j = 0;
bool notFound = true;
while ((stringABC[j] != '\0'winking smiley && notFound)
{
if (stringCheck == stringABC[j])
{

notFound = false;
stringCheck = j;
vectorEncrypted.push_back(j);
}
else
j++;

}

}
cout<<endl<<"The encrypted string is ";
for (int i=0; i<vectorEncrypted.size();i++)
{
cout<<vectorEncrypted;
cout<<endl;
}

return 0;
}
avatar Re: June 2009 exam paper...Question 2.
November 12, 2009 10:30PM
Watch out with the statement:

if (stringCheck == stringABC[j])

This will not do what you think it will. Once again, you're still ignoring the fact that stringCheck is an array. This time, however you're comparing a char * to a char. A char is one of the integer types and is 8 bits long (0..255 unsigned, -127..126 signed) whereas a pointer is an integer, 32 bits on a 32-bit machine.

Your compiler will likely not complain about comparing a pointer to a char because it knows how to convert an 8-bit integer to a 32-bit integer and therefore they can be compared. The danger here is that the comparison will yield unpredictable results and has nothing to do with what you intended.
Anonymous User
Re: June 2009 exam paper...Question 2.
December 07, 2009 05:56PM
Hi guys

I need the COS112V june 2009 exam paper. As well as the INF207E November 2009 exam paper. I desperately need these.

My email is : jmurindagomo@gmail.com
Sorry, only registered users may post in this forum.

Click here to login