WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

1) Assuming you need to "access each number one at a time", put the program in a loop. Inside the loop, read a number, use it, then when the loop returns to the beginning, the next number will be read.

2) If you need to use multiple numbers at the same time, the only appropriate way is to use an array. But from your description, this is not your case.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Use getche() in place of scanf. why scanf is not working, answer of this question
is here

http://www.gidnetwork.com/b-59.html

repz=getch(); 
or 
repz=getche();//echo the character on screen

Best Of Luck

Yeah, Best of Luck. Since getche() is a non-standard function, it's use is suspect and not recommended. It has been defined in only a couple of the hundreds of compilers created.

And I already told him where the answer is :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Read your input as a string. Then test the characters for valid digits. If good, convert to integer. If not...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Read this series on scanf(). It should answer your questions.

N1GHTS commented: I admire your simple yet effective method of solving problems. +1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What makes you think so?

200 * .09 = 18
18 + 200 = 218

Exactly what was printed.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Use the curlys.

An alternate of version 1, and the format I prefer:

while(1) 
{
    if(1) 
    {
        i++;
    }
}

I personally find that in your original style I tend to loose the {, but in this version the block is visually better defined. Again, my preference. Both work just as well.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I'm actually quite interested in a robust method if you don't mind. I want to build good habits as a programmer.

Good habits as a programmer start with understanding the functions you are using and using them properly to get the results you want -- not by searching for a function that does exactly what you wish.

The most robust method is processing each key individually and doing exactly what you need to do with each character entered.

I want people running this program in a Windows C compiler to be able to see the output before their window closes, so I added the line "std::cin.get();" before "return 0;", but now I receive the error message:

Isn't that C++? Why would you add a C++ function to a C program? And one that is identical to getchar() too?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

First let me say, please don't post code back all fixed. If you see where the error is, just tell me where it is and I will look to find it. Thanks, much appreciated!

Where what is? Don't you think it would be helpful to tell us what we're looking for?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Except for using feof() (see this) the code looks fine. More info is needed.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hello,
I am trying to make a program which will sort a array of 10 elements using bubble sort. I searce dthe forum and google but douldnt find a problem similar to mine.

You're kidding, right? No one site that explained how the Bubble Sort works sorted numbers? I think you need to search again. Try searching for "Bubble Sort". The first 5 hits show exactly your stated problem.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

In f2c(), 5/9 = 0. In c2f(), 9/5 = 1. Remember the limits of integer math...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

It looks like the only thing you need to do is give the array marks a size and you should be good to go.

You do not need vectors. You do not need malloc().

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Because every time a word from array1 does not match a word int array3, it's added to array3.

So, if you have 4 words in array3, and the current word in array1 matches none of the words, the word will be added 4 times to array3. You now have 8 words in the array.

Look closer at how your loop and IF statements works. You only want to add the word after you've looked at all the words in array3

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Since you claim Banfa is wrong, you need to explain how it crashes. We cannot help you fix a crash without knowing the error message explaining the crash.

Much of your problem deals with scanf() . Read this series to understand how to deal with the function when reading numbers and single characters.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You'd be much better off reading a line into a structure, then writing the structure. Very simple. One record after another.

You can't just blindly use fseek for the sake of using fseek and expect a file to have any useful format at all. If you back up in a file and write, you are writing over file data.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Only call srand() once at the beginning of the code.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The length of the search string points at the '='. Use that as a guide.

iamthwee commented: Repetive advice kiddo, read before you post. Oh and just have faith. . . believe me :) -2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

-- tongue -- :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

In other words, feel free to post your questions in the forums mentioned.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

No, just read the file one line at a time.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Read this

And start using CODE Tags.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Format your code better and the problem will smack you in the face...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

But it's for a thread, not a post. It belongs at the bottom of the thread, maybe avove the reply box for easy access.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

2. You will need to call CheckWin() after CheckMove() as user's move can win the game. Based on your current logic, computer will make a move even if the user's move wins the game which is not right. Furthermore, the computer's move might eliminate the user's winning move.

Unless, of course, you write the game properly. In this case it would be impossible for a user to win the game, which makes a call to CheckWin() useless. Any player that actually knows the game can never lose -- including the computer.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

When you create a Function, set the return value to the function name to return it:

Function GetValue(aVal as Integer, bVal as Integer) as Integer
...
    GetValue = answer
End Function

Use: newValue = GetValue(a,b)

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Don't know. What it do wrong? If you want help you really need to explain. Read the Member Rules.

The thing I noticed is your srand() is in the wrong place. Put it at the beginning of the program so it's called only once.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Don't and I mean don't ever use

gets(myString);

Read Please
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

I will add to that "Never use scanf("%s",...) and scanf("%c",...) ". See this series for an explanation why.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Open the file in binary mode and use binary output functions, not fprintf()

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

A local variable (str3) is deleted once the function ends.

First, loop through the to string to find the \0.
Second, now loop through from copying each character to to.
Return to.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you are inputting into an integer variable, you cannot test for characters because they can't be entered into an integer.

If you input into a string you can then test if the values are digits or not.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Your for-loop is wrong ..

void getRaise(double pay[], int size)
{
// should rather be ..
for(int x=0; x <size; ++x)
...

To restate, since it took me a while to figure out exactly what you meant, take out the semi-colon at the end of your loop definition.

mitrmkar, please be a little more explicit next time. Noticing one minor character change can be difficult to spot without pointing out the change.

at the end of int main i usually put:

system("pause");
return 0;

it means you have to press a key before the console closes

Bad suggestion. Why call the operating system to pause a program when a simple cin waits? And pause doesn't exist on all operating systems.

The OP used getch() for that very purpose anyway, also not recommended. Why include a C header and a non-standard function for this simple task? Again, cin does it. :icon_wink:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

no, plagiarism is me handing you part of a text created by someone else.

No, plagiarism is you USING the document as your own. Theft is you giving something you don't own to someone else -- sort of. It's definitely not plagiarism.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
#include <ctime>
time_t t;
     time(&t);
     srand(t);

Or shorten it to simply:

srand(time(NULL));
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

fflush(stdin) -- See this
getch() -- See this
while (!feof(F1)) -- See this

BestJewSinceJC commented: Good help to the OP +5
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Or
use fgets() to read the file line by line.
test each character to see it they are all digits
if all digits, copy the line to an array to be sorted and output.

There is no need to convert the numbers to ints although it might make the sorting easier.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

No. Copy the answer code to tmpAns. Then test for black in a loop with:

If guess(i) == tmpAns(i)
    black = black + 1
    tmpAns(i) = -1      // answer code is found, don't reuse
    guess(i) = -2       // guess has been used, don't reuse

That's the entire black test.

White test takes a nested loop, but it's identical..

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you say so. All I know is my version uses a total - that's total - of 3 arrays, and 5 variables.

char  pattern[NUMVAL];     // The answer key
    char  patterntst[NUMVAL];  // Temp version of the answer key
    char  guess[NUMGUESS];     // The user's guess
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

WaltP your way doesn't even work... you need the 2nd array to say where you already guessed that position or not.

Sure it does. You only need to test the current values entered against the answer key. Based on what I see in your code, it's 10 orders of magnitude more complex than it needs to be.
bool**, malloc'ed bool arrays, triple nested loops -- all unnecessary. You need to think simple.

And in MM, you don't return the positions correct, only the count. I don't think the 2nd array is necessary.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

In addition, you cannot read into argv. That's undoubtedly where it's going wrong.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

So I think i figure it out... But when my population is too big the answers are screwed up. :(.

Here's my code:
Main

int Mastermind::getblackpegs(int* guess)
{
	int blackpegs=0;
	for(int i=0; i<4; i++)
	{
		if(guess[i]==answer_[i])
		{
			blackpegs++;
		}
	}
	return blackpegs;
}

int Mastermind::getwhitepegs(int* guess)
{
	int whitepegs=0;
	bool guesscounted[]={false,false,false,false};
	bool keycounted[]={false,false,false,false};
	for(int i=0; i<4; i++)
	{
		for(int j=0; j<4; j++)
		{
			if(guess[i]==answer_[j]&&!keycounted[j]&&!guesscounted[i])
			{
				whitepegs++;
				guesscounted[i]=true;
				keycounted[j]=true;
			}
		}
	}
	whitepegs=whitepegs-getblackpegs(guess);
	return whitepegs;
}

That's soooooo much better. Short and sweet.

White Pegs is a little odd.
1) You only need one boolean array, not 2.
2) Outer loop should go through the answer pegs.
3) Inner loop through the guess pegs.
4) The boolean should match the guess peg/inner loop.
5) Return the number of pegs that match (change the name of the function)

This way you have two function calls in the calling function:

npeg = getMatchingPegs()  // get the number of pegs that match
bpeg = getblackpegs()     // get the number of pegs that match in position
wpeg = npeg - bpeg        // get the number of white pegs

This keeps the functions simpler and you don't have to call the black peg routine twice.

By the way, what's with all the underscores?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You don't need to complicate it so much.

Loop 1 through the answer array i=0-3
    Loop 2 through the input array j=0-3
        if answer[i]=user[j]
                negate user[j] (flag as seen)
                increment count
                exit loop 2
    next j
next i
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

1) Count the numbers that occur in both arrays -- correct pegs.
2) Count the numbers that occur in the correct position in the arrays -- black pegs.
3) Subtract the black pegs from the correct pegs -- white pegs

Answer: 2658
Guess: 6625

Correct: 3
Black: 1

Therefore-
White: 2

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
myfunc(&array[0]);

This complexity is not necessary. myfunc(array); is enough.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

strcat()

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

strncpy_s(userId, email_address.c_str(), 9); copies your email domain into user ID. Whatever was there is overwritten.

By the way, just use the strncpy() function. strncpy_s() is non-standard, non-portable, not a good choice to use.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Did you remove the trailing '\n' from your input string using fgets() ? It's always good to examine the data read to see if there are anomalies in your console input. There usually are... strstr() is used to search a string. It looks like you simply need strcmp() .

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I have changed a few things around. Still not completely working.

Maybe you're using the wrong logic.

If you want to ask a question, ask a detailed question and you'll get help.

If you want us to say "Awww, poor program :'(" post a statement like you did with no indication of what's not working.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What two things are wrong with this line scanf("%d", &abc[30]); ???

hint for 1: you have defined abc as a character array, didn't you?

You need to study the printf() function closer...

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

can any1 give me a tip ..?

Sure. Explain exactly what happens and what should have happened. Tell us where the problem is so we have an idea where to look.

In general, if you want help, explain in detail what the problem is. Don't leave it to us to figure what's happening. Tell us.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Umm, we haven't reached the point in the class which tells us how to read in a string and convert or reorder data.

Reordering the data take an editor. You edit the file so the data is in a different order so you can read it properly using the program.

Can I use getline(infile, title)?
Thank you

Yes.