0

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.

0
  • Which compiler are you using ?
  • Have you created a console application ?

  • Is this the code you entered:
    [CODE]

    include <iostream>

using namespace std;

int main()
{
int age = 0;
cout <<"please enter your age :";
cin >> age;
age = 10 + age;

cout <<"your age in ten years will be"<< age << endl ;

return 0;

}
[/CODE]

0

[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 !

0
  • 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.
0

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.

0

Ive run your program and it gave me an error, according to my limited knowledge it's related towards going out of bound with your array.

Try using for loops:

[CODE]for (int i = 0; i < (N-1); i++)[/CODE]
and change your switching algo to this:

[CODE]
if (a[j] < a[j+1])
{
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}[/CODE]

Remember, you need two loops, you run the first for one time and the second will go threw the whole array, if that's okay, your first array will run for the second time and so on and on.

Watch out for overflowing your array.

0

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?

0

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

Hello ladies and gents,

Got a question, is it wise to create a member of a class by using another class it's constructor and deleting it threw the use of that class it destructor?

For example:

[code=cplusplus]// Testing code.

include "first.h"

int main()
{
first myMenu;

bool gameLoop = true;

while (gameLoop)
{
    gameLoop = false;
}

return 0;

}[/code]
[code=cplusplus]#include "second.h"

class first
{
public:
first();
~first();

private:
second *mySecond;
};[/code]
[code=cplusplus]#include "first.h"

first::first()
{
mySecond = new second;
}

first::~first()
{
delete mySecond;
}[/code]
[code=cplusplus]class second
{
public:
second();
~second();
};[/code]
[code=cplusplus]#include "second.h"

second::second() {}

second::~second() {}
[/code]

Is this something that is done or should it be avoided?

Thank you.

0

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 :)

0

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

0

[QUOTE=Dave Sinkula;304912]Forgive me, but without declarations and the intent of the operations, there is really not a question being asked here.[/QUOTE]

Hi Dave, I understand what you're saying, but the examples that I gave are how they are given in the exercise for the Bjarne Stroustrup The C++ Programming Language, there isn't anything mentioned about declarations or intent of operations.

I guess it's just an exercise to practise your knowledge of order of precedence for the various expressions.

@ Walt, thanks for that Walt, but, as said previous, don't think this exercise means much more then to see whether you know what expression has a higher precedence.

Thanks for the help guys ;)

0

Hello ladies and gents,

Ive got a few more expressions I needed to fully parenthesize:

1) p++ becomes (p++)

2) --p becomes nothing
if
--p would have been --p:
--
p OR--(*p) would have the same result.
Reason is, both would have the same precedence, so, it
goes from left to right in both cases.

3) ++a-- becomes ++(a--)

4) (int)p->m becomes (int)(p->m)

5) p.m becomes (p.m)

6) a[i] becomes (a[i])

One more question, am I correct that the following expression:
(int*)p->m equals a pointer member which get's a type cast conversion?

0

[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:

0

[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?

0

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.