Well, whatta you know, I have a Home Key aswell :D

Thanks Narue ;)

Hi Danny,

Was wondering when you scrolled down all the way to read the thread you're interested in, there's no way to either go to the top of the page or push a button to go back to view the forum in which the thread was started ?

Wouldn't it be alot easier and faster then having to scroll back up every time.

Great tutorial on pointers by Narue @ [url=http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx]Eternally Confuzzled[/url]

[QUOTE=clutchkiller;733096]Yes i understand i need to use break; =P

So what if i make cho an integer? Then it would be 1 and not '1' correct?

  • the labels must be literal characters.

I got a long way to go lol. Thanks in advance[/QUOTE]

Yep, like vmanes mentioned !

  • char cho [B];[/B]

  • After each [I]case[/I] you have to use a [I]break;[/I] , otherwise, the program will fall threw to each case that is presented.

  • I guess you haven't shown all the code, seeing as you didn't use close braces for the do loop and main.

1 is a number.

'1' is a character.

So, it depends on which of the two you are going to use.

Great tutorial on pointers: Narue's [URL="

Darn, sorry Narue. As soon as I saw your correction to my code, I felt stupid :icon_redface:

Hi Narue,

Well, the problemis that I'm getting these error messages:

[TEXT]Removed error messages[/TEXT]

Hi all,

I have a class which contains a struct that holds some variables, string, int's.

Now, one of the variables (string) in the struct is used to compare it with a name, if the name is in the struct, it has to be [COLOR="Red"]returned from my function [/COLOR]with it's own integer variables to be put into a separate list.

The thing is, how is the code written to do so ?

[CODE]struct ArmorArray
{
std::string armorName;
int armorCheck;
int maxDext;
int price;

ArmorArray *nxtArmor;

};

class Armor
{
private:
ArmorArray ArmorList[13];
ArmorArray *p_List;

int m_SIZEARRAY;

std::string m_ArmorName;
std::string m_String;

public:
Armor();
~Armor() {}

[COLOR="red"]p_List [/COLOR]BuyArmor();
std::string CreateString(std::string &myString);
std::string ArmorBought(std::string &bought);

};[/CODE]

[CODE][COLOR="Red"]p_List [/COLOR]Armor::BuyArmor()
{
m_ArmorName = CreateString (m_String);

for (int i = 0; i < m_SIZEARRAY; i++)
{
    if(!m_ArmorName.compare(ArmorList[i].armorName))
    {
        return [COLOR="red"]ArmorList[i];[/COLOR]
    }
}

//return m_ArmorName = "Armor does not exist ! Try again or return to previous menu !";

}[/CODE]

Thanks for any assistance.

Ok, thanks for the explanation vmanes.

Well, found a solution to solve the problem:

[CODE]
if (fin) // allready exists ?
{
cout << "Current file contents:\n";
char ch;

    while (fin.get(ch))
        cout << ch;
    cout << "\n***End of file contents.***\n";
}
fin.close();
fin.clear();

.......
[/CODE]

The problem seems to be this:
[QUOTE]fin returns 0 because "the base class iso (from which istream is inherited) provides an overloaded cast operator that converts a stream into a pointer of type void*. The value of the pointer is 0 if an error occurred while attempting to read a value or the end-of-file indicator was encoutered." (1)[/QUOTE]

So, If I understand this correctly, your input because of being casted to a type void pointer get's another address and therefore isn't recognised?

If so, why does the function clear() solve this?

Good evening ladies and gents,

Had a question concerning this piece of code that I'm trying out from a book, it supposed to open a file, write text to it, close it, reopen it, append text to it and then write the text from the file to the console screen, it works fine up untill the text has to be written to the console screen, then I get the message that the file can't be opened for reading though the text is added to the file?

What am I doing wrong?

[CODE]

include <fstream>

include <iostream>

using namespace std;

int main() // returns 1 on error
{
char fileName[80];
char buffer[255]; // for user input.
cout << "Please reenter the file name: ";
cin >> fileName;

ifstream fin(fileName);

if (fin) // allready exists ?
{
    cout << "Current file contents:\n";
    char ch;

    while (fin.get(ch))
        cout << ch;
    cout << "\n***End of file contents.***\n";
}
fin.close();

cout << "\nOpening " << fileName << " in append mode...\n";

ofstream fout(fileName, ios::app);
if (!fout)
{
    cout << "Unable to open " << fileName << " for appending !\n";
    return 1;
}

cout << "\nEnter text for this file: ";
cin.ignore(1, '\n');
cin.getline(buffer, 255);
fout << buffer << "\n";
fout.close();

fin.open(fileName); // reassign existing fin object.
if (!fin) // <-- problem is here.
{
    cout << "Unable to open " << fileName << " for reading !\n";
    return 1;
}

cout << "\nHere's the contents of the file: \n";
char ch;
while (fin.get(ch))
    cout ...

Hi Narue,

Great to see you're stilll around here.

It looks fine, why do you ask?
Because I wasn't sure Narue.

It's just your debugger's way of saying that mName isn't ready to be used yet. If you get that [b]after[/b] stepping over the line (where mName should be created and initialized), then you probably have an issue.
Ah, ok, theres no problem after stepping over the line.

Why should you? std::string is an object that manages the actual string for you. The memory grows to fit the size of the string automagically.
Well, believe it or not, I thought it wasn't necessary, but, because the message of the <bad Ptr> I started to doubt this. I was thinking because I didn't add a sizeof, the <bad Ptr> was appearing.

Yes, it's gimped now. Completely useless if you want to do any kind of advanced searching. But it seems Dani isn't done upgrading the search feature, so perhaps it will be fixed in the near future.
Well, I hope she fixes it, it was alot better and easier to find related posts/threads to what you where looking for !!!

Anyway, thanks again Narue, good to see you're still around :)

Hello ladies and gents,

It's been ages since I posted here, have been doing some coding on and of and I was wondering about the following, beneath is a test I made in trying to use the new operator in combination with a string, first a string literal, second time a string input.

It's working, but, the thing that I'm seeing when debugging this is when I get to the point [inlinecode]std::string mName = "John Malkovich";[/inlinecode] the value of mName comes up as <bad Ptr>.

Questions:
A) is the way I used the string and new operator correct for this little program?
B) is this <bad Ptr> related to a bad Pointer and what does that mean? Probably not something good?
C) It seems that when I use [inlinecode]std::string *pName = new std::string;[/inlinecode], I don't have to determine the sizeof this string? Is this correct? Or is something going wrong which I'm not aware of?

[code=C++]
// Testing code.

include <iostream>

include <string>

int main()
{
std::string mName = "John Malkovich";

std::string *pName = new std::string;
*pName = mName;

std::cout << "Name is: " << *pName << " !\n";

delete pName;
pName = 0;

std::string secName = "";

getline(std::cin, secName);
pName = new std::string;
*pName = secName;

std::cout << "Second name is: " << *pName << " !\n";

delete pName;
pName = 0;

return 0;

}[/code]

Thanks for any help you guys/girls can help.

Edit: oh yes, has the search function been altered on this forum? I seem to remember ...

[QUOTE=Ravalon;304133]I'm not Dave, but hi anyway! :)[/QUOTE]

Hi Ravalon :cheesy:

[QUOTE]Yes, that's it exactly. :) [/QUOTE]

Ok, that's what I needed to know.

[QUOTE]New code should be written in either C95 or C99. Okay, maybe that wasn't so easy. :eek:[/QUOTE]

When you speak of "new code", you do mean "new code" written by those compiler developers right? If not, then yeah, you got me confused again :cheesy:

[QUOTE=Dave Sinkula;303979][intervening distractions]

I also meant to link to this for a C90 list:
[url]http://web.archive.org/web/20050207005628/http://dev.unicals.com/papers/c89-draft.html#A.6[/url][/QUOTE]

Hi Dave,

You're speaking of the C90 list, but yet, the Draft shown is C89, am I missing something?

Also, what is actually C89, C90, etc... all about, are they like definitions of what C/C++ compiler writers have to keep in consideration?

Thank you both for the help gentlemen :!:

Nope, can't find it, opened parent directory, searched for anex J, doesn't seem to be in the list.

Hi Salem,

Thanks for the link, but, to me, it reades rather abstract and doesn't really tell me if what I wrote was correct.

Can you tell me whether the following examples are examples of undefined and implementation-defined constructs:

// Implementation defined:

1) unsigned char c2 = 1256; // implementation defined.
2) char ch = 'j'; char *p = ch; // implementation defined.

// Undefined behaviour:

1) num /= 0; // undefined behaviour.
2) int arr[2] = {0};
for(int i = 0; i < 4; i++) // undefined behaviour.
arr[i];
3) int arr[2] = {1, 2, 3}; // undefined behaviour.

Hello ladies and gents,

Gotta few questions if you don't mind:

1) Exc_6_4) Wrote a table of values for the bitwise logical operators and was wondering if any of you could check them out whether they are correct this way.

2) I have to write five examples of undefined behaviour and five examples of implementation-defined behaviour. I'm I correct in assuming that undefined behaviour happens when you've written code that is although accepted by your compiler, doesn't behave as you intented or even let's your computer crash and that implementation-defined is when you've written code for which your compiler gives you a warning about?

[QUOTE=Bench;297755]Try writing a 'hello world' app or something simple just to see if it works in there.[/QUOTE]

Yep, that did the trick Bench, thank you :!:

I do believe it is related to Intellisense, because when I enter for instance
std:: I get a message: 'No additional information available'. It's that information I want again.

In the past, when I entered std:: --> I got a list with all possibilities, the same for instance with cin. --> I got a list with for example get, clear, ignore, etc...

It's not the shortcut Ctrl-J I'm after, it's the above.

Hello ladies and gents,

Was wondering if any of you could help me out with the following, sometime ago I had to reinstall MS V++ EE and ever since then, when I enter for instance 'std::' or 'cin.' I don't get a list with all the possibilities it has, normally I had like a list for cin. like get, clear, ignore, etc...

Can any of you tell me how I can activate that again? Hope you understand what I mean, was difficult to explain it correctly.

Thank you.

iamthwee, thank you very much for that link :!:

Made this from the example you gave:

[CODE]for (size_t i = myWord.find(mySecArr, 0); i != std::string::npos; i = myWord.find(mySecArr, i))
{
count++;
i++; // Move past the last discovered instance to avoid finding same string.
}[/CODE]

The [INLINECODE]i++;[/INLINECODE] is really necessary, otherwise the loop just keeps on going, am I correct that as the text stated, when not using this, the string never fails finding the char and therefore [INLINECODE]i != std::string::npos; [/INLINECODE](search failes) is never reached?

Maybe the C++ version was supposed to use .find()?

HI Dave, long time no see, I see you lost your mod stick, good to see you here again though :)

I tried using .find() and .compare(), but it seems that they can only be used to find if there is A resemblance of code in both strings, not several. Could it be that I have to write it in a way it traverses threw the string that I want to compare? If so, I can only traverse threw one by using a loop and either string[i] or *(string + i) right ?

This is enough room for one character plus the terminating null:
char letters[2];

DARN, I forgot the terminating null, it does work though, I'll change it to [3] and add the '\0' at the back, thanks for the tip.

There are other ways, but not necessarily better. For just two characters yours way is efficient.

Oh, ok, thanks.

If you had to test 4 or more, a loop might be better.

Nope, don't understand that, I thought I was using a loop?

There are also the C-string str???() functions and C++ strings compare methods. Either of those you can look into.

Thought so, thing is, as said above, how do I use .find() and/or .compare() when having to go threw the whole string when trying to find more then one occurence of the same piece of string?

Hi guys,

Solved the exercise, but, I'm not that happy with it because both functions actually look very similar, meaning, the c-style string and the cpp string. Don't think that was the idea of this exercise.

[CODE]//Exercises from the book: The C++ Programming Language by Bjarne Stroustrup.

/Write a function that counts the number of occurences of a pair of letters in a string and
another that does the same in a zero-terminated array of char(a C-style string). For example,
the pair "ab" appears twice in "xabaacbaxabb".
/

include <iostream>

include <string>

int compareChar(char myArr, char mySecArr);
int compareString(std::string myWord, char mySecArr[2]);

int main()
{
char mychar[] = "xabaacbaxabba";
char letters[2];
std::string myWord;

myWord = mychar;

std::cout << "Enter two letters to be compaired with the char string '" << mychar << "'" << std::endl;

std::cin >> letters;

int count = compareChar(mychar, letters);
std::cout << "The pair '" << letters << "' appears " << count << " time(s) in " << mychar << "." << std::endl;

std::cout << "Enter another two letter to be compaired with the string string '" << myWord << "'" << std::endl;

std::cin >> letters;

count = compareString(myWord, letters);
std::cout << "The pair '" << letters << "' appears " << count << " time(s) in " << myWord << "." << std::endl;

std::cin.ignore(2);

return 0;

}

int compareChar(char myArr, char mySecArr)
{
int count = 0;

for (int i = 0; i < strlen(myArr)-1; i++)
{
    if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1]))
        count++; ...

[QUOTE=WaltP;287900]I would reverse the -1 and go with:
[INLINECODE]int strLength = strlen(myArr);[/INLINECODE] and [INLINECODE]for (int i = 0; i < strLength-1; i++)[/INLINECODE]
The reason is [I]strLength[/I] actually represents the string length, which you can modify in the loop. And you can still use [I]strLength[/I] elsewhere in your program if the need arises. By subtracting 1 first, it's really only useful for this particular part of the program.[/QUOTE]

Yep, that seems to make better sence, thanks Walt.

Hmmm, well, I came up with this [INLINECODE]int strLength = strlen(myArr)-1;[/INLINECODE] and then in the loop [INLINECODE]for (int i = 0; i < strLength; i++)[/INLINECODE] but I see where you're coming from :o

Thanks for the example, I'll see that I can find an equivalent to use in my program next week-end.

Hi guys,

This exercise goes as follows:

  • Write a function that counts the number of occurences of a pair of letters in a string and another that does the same in a zero-terminated array of char(a C-style string). For example, the pair "ab" appears twice in "xabaacbaxabb".

Now, I found the following solution to the C-style version:
[CODE]#include <iostream>

void compareChar(char myArr, char mySecArr);

int main()
{
char mychar[] = "xabaacbaxabb";
char letters[2];

compareChar(mychar, letters);

std::cin.ignore(2);

return 0;

}

void compareChar(char myArr, char mySecArr)
{
int count = 0;

std::cout << "Enter two letters to be compaired with the char string 'xabaacbaxabb'" << std::endl;

std::cin >> mySecArr;

while(strlen(mySecArr) != 2)
{
    std::cout << "Wrong amount of letters, try again!" << std::endl;
    std::cin >> mySecArr;
}

for (int i = 0; myArr[i] != 0; i++)
{
    if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1]))
        count++;
}

std::cout << "The pair '" << mySecArr << "' appears " << count << " time(s) in " << myArr << "." << std::endl;

}[/CODE]

I was wondering if any of you could tell me how I can improve this, the reason for asking is that at the end of the last comparison [INLINECODE]if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1]))
[/INLINECODE] the [INLINECODE]myArr[i+1][/INLINECODE] goes out of bound. Isn't that bad coding?