Bob 15

On a simple program that any reasonably competent C++ beginner could write? You might find a few people who are interested, but they're all going to be beginners like you who have homework similar to your program's description.

I wish you wouldn't speak for everyone, because you don't. I'm not a beginner, and I'm interested. I contributed to the earlier thread, I respect the OP for coming back with an improved attempt, and in keeping with the spirit of DaniWeb, I'd be more than happy to offer further support with it.

I think you need to adjust your opinion of this program to be more realistic. You're 14 (according to your signature), so it's understandable that you think the world revolves around you and everything that you produce is magic,

Respectfully, I don't think that's called for.

but I'm willing to bet that most of the people who see this program will think the same thing I did. It's quaint, and props to you for doing it, but it's clearly written by a beginner and I could quickly write a better version with minimal effort. Therefore, I have zero interest in either the original program or updates.

Anyone who has no interest in it surely only needs to skip the thread?

If you have any specific questions about how to improve the program, I'll be happy to offer some opinions. However, I'm not going to lavish you with praise, which is clearly what you're expecting by ...

Bob 15

temp.shillings = pe / 12;
temp.pence = pe % 12;

Yeah, does that help? How is your output?

Now imagine that instead of typing 12 or 20 or 240 everywhere within your code, you replaced those literals with a named constant, for example:

const int PENNIES_IN_POUNDS = 240;
const int SHILLINGS_IN_POUNDS = 20;
const int PENNIES_IN_SHILLINGS = 12;

The lines above then become:

temp.shillings = pe / PENNIES_IN_SHILLINGS;
temp.pence = pe % PENNIES_IN_SHILLINGS;

If you go one step further and use a more meaningful name rather than the abbreviation 'pe', you have something like:

temp.shillings = pennies / PENNIES_IN_SHILLINGS;
temp.pence = pennies % PENNIES_IN_SHILLINGS;

You've replaced the magic numbers (12 in this case) with constants with meaningful names (PENNIES_IN_SHILLINGS).

Bob 15

should I do the remainder of both? and not divide.

No, just double check what your expression is for. You're calculating how many shillings there are in a given number of pennies, but you're diving by the number of shillings in a pound (20) rather than how many pennies in a shilling (12) -- even though you correctly use 12 on the next line.

Bob 15

Here's a thread with a slightly different but related issue here that will probably help (if you scroll down to the bottom):

http://www.daniweb.com/software-development/cpp/threads/22885/1-cin-statementmultiple-values-to-a-vector

Bob 15

Just a note to add that if the task you've been set is to do this with pointers, you can get rid of integers i and j and does this just through three pointers front, rear and temp.

Bob 15

Here:

*front = entry[i];

this assigns the character at entry[i] to the memory at the address represented by front, but front hasn't been allocated any memory. I suspect what you want is to assign to the pointer the address of the character, like this:

front = &entry[i];

The same applies to the line that follows, with rear.

Watch out also for this:

 while (i != j)

because depending on whether there is an odd or even number of characters i may never equal j.

Bob 15

warning C4700: uninitialized local variable 'n' used line 39

 32.   int cost[10][10],i,j,k,n,c,visit,visited[10],l,v,count1,vst,p,dup1,dup2;

 39.  for(i=0;i<n;i++)

You're using n in an expression in line 39, for (i=0; i<n; i++), but it hasn't yet been initialised, so it holds an unknown garbage value. When i is compared with n, you're comparing i with whatever garbage value n holds. The compiler, understandably, is warning you of this potential problem, which if unresolved can lead to a bug and possible obscure behaviour.

Bob 15

Oh. They deleted the thread with the response in and kept this one? That wasn't very smart.

WaltP commented: Then don't post in BOTH threads. +14

Bob 15

Q1. What is 'sf' ? Is it a class ?

As per myk45's post, it's a namespace, in the same way that std is a namespace. If you're not up to speed with namespaces, a quick check in your C++ reference book should tell you what you need to know.

Q2. Why is the operator '::' being used here ?

In addition to what myk45 said, it's a scope resolution operator. In conjunction with the namespace name it identifies that Clock is from the sf namespace, rather than any other possible Clock that might exist in a different namespace.

Q3. Is 'Clock' a class as well?

Because it's in the SF namespace and part of SFML, if you want to know more about Clock, you should check out the SFML documentation.

Q4. what does the 'f' represents in sleep(0.5f)?

See myk45's post.

Q5. Why can't we use 'getch()' in visual studio? (I am a beginner)

In addition to what myk45 said, each time someone uses getch() a kitten dies.

Some of your questions above relate to standard C++, some are SFML-specific. It'd be worth familiarising yourself with the SFML documentation and checking out any SFML-specific support forums.

Bob 15

What output are you seeing?

I don't use SFML, so maybe there's someone better qualified to support you here (or on an SFML-related site). But it looks to me as though you have two threads running, the main thread and a second thread that you launch, and each writes out statements from within a loop. I'd expect to see some variation in the order of output statements being written out, depending on how the threads run.

Bob 15

Hi Nathan Drake,

You know what format input to expect, that's pretty well defined for you, so why not figure out how you intend to store it, then how you're going to read it in. That will get you started, at least.

myk45 commented: haha, the OP's question mentions Bob! :D +0

Bob 15

I haven't checked your code yet, but what kind of problems are you having? Does this compile? Only, I'd expect to see a semicolon terminator for your structs, which makes me wonder if you're fighting compilation errors. e.g.

struct Athlete
{
    int aID;
    string aName;
    int aAge;
    char aGender;
    string aCountry;
};

Can you elaborate on what errors you're seeing?

Bob 15

Which code do I have to use so the program is able to see at least 4 characters and won't crash if I enter a letter instead of a value?

Your problem is that cin goes into a failed state when it gets the unexpected input (e.g. letters when it's expecting an integer value). The advice I posted earlier will enable you to detect when cin goes into a failed state (cin.fail), clear the fail state (cin.clear), and get rid of whatever characters are left in the stream (cin.ignore).

Technically you can, as an alternative, store and parse the input, and there are times when parsing input can be worthwhile, but you don't need to for simple input like this.

If you don't understand the explanation about cin going into a failed state, that's fine. Just look up cin.fail(), cin.clear(), and cin.ignore(). It'll take you 5 minutes and you will have your solution.

markwiering commented: Thanks! With your advise, I was able to update my program. Now the program won't allow you to enter characters or symbols when values are required. +1

Bob 15

Hi silvercats,

That other thread includes some fairly recent posts about some fairly recent books, so it's worth a read. You can check out the books mentioned on Amazon, use the preview to check whether the content and style looks right for you, for where you're coming to C++ from. That would be my advice: go back to that thread, read some of the more recent recommendations, check them out on Amazon, see what appeals to you. At that point, you can always ask here in Daniweb whether anyone has experience of a particular book that interests you.

One of the more recent recommendations in that other thread is for C++ Primer, now in its 5th edition, so the book has legs, and it's been updated to include features from the latest C++ standard, C++11. If that's something that's important to you (and why wouldn't it be?) perhaps consider C++ primer as a candidate. It includes exercises. I'm not sure if includes answers, but you can always ask here in Daniweb if you're having trouble with an exercise.

Good luck with your search, and with your C++.

Bob 15

It's good that you found something that worked. However, you may be interested in finding out more about escape sequences, which allow you to use:

uArray[2] = '\''; // single quote + backslash + single quote + single quote

There are others, besides this one for the single quotation mark, such as:

\" for double quotation mark
\? for question mark
and a double backslash to represent the backslash

One of the most common that people come across is \n for newline, perhaps not realising that it belongs to a group of such escape sequences. A quick search on Google with give you a list of them.

Bob 15

[QUOTE=fishman]I need to pass the whole array of objects to a function where I can play with the attribute values or change them if necessary.

I have been playing with pointers and references but can not get them to work with an array of objects like I have.[/QUOTE]
Just pass the array of objects to the function by passing the array name as a paramater. Just like passing any other array. You can then access the objects in the array in the same way you would any array element. For example, not using your code exactly but a simplified example:

[code]

include

using namespace std;

class fish
{
public:
fish();
int getAge() {return age;}
void setAge(int newage) { age = newage; }
private:
int age;
};

fish::fish()
{
age = 0;
}

void showAges(fish fishes[])
{
cout << "fish 1 age is: " << fishes[0].getAge() << endl;
cout << "fish 2 age is: " << fishes[1].getAge() << endl;
cout << "fish 3 age is: " << fishes[2].getAge() << endl;
}

int main()
{
fish myFish[3];

showAges(myFish);
cout << endl;

myFish[0].setAge(10);
myFish[1].setAge(20);
myFish[2].setAge(30);

showAges(myFish);

cin.get();
return 0;

}[/code]

Bob 15 using namespace Bob

[B]Intro[/B]

This tutorial provides a brief introduction to the random number functions that come as part of the C++ standard library, namely rand() and srand().

[b]rand() and RAND_MAX [/b]

The C++ standard library includes a pseudo random number generator for generating random numbers. In order to use it we need to include the header. To generate a random number we use the rand() function. This will produce a result in the range 0 to RAND_MAX, where RAND_MAX is a constant defined by the implementation.

Here's a piece of code that will generate a single random number:

[code]#include

include

using namespace std;

int main()
{
int random_integer = rand();
cout << random_integer << endl;
}[/code]The value of RAND_MAX varies between compilers and can be as low as 32767, which would give a range from 0 to 32767 for rand(). To find out the value of RAND_MAX for your compiler run the following small piece of code:

[code]#include

include

using namespace std;

int main()
{
cout << "The value of RAND_MAX is " << RAND_MAX << endl;
}[/code][b]srand() [/b]

The pseudo random number generator produces a sequence of numbers that gives the appearance of being random, when in fact the sequence will eventually repeat and is predictable.

We can seed the generator with the srand() function. This will start the generator from a point in the sequence that is dependent on the value we pass as an argument. If we seed the generator once with a variable ...

Bob 15

Surely the best person to ask would be your instructor?

Bob 15

To delete a file use the C++ remove() function.

Bob 15

budweiser --> frog