hag++ 24 Junior Poster

Hey, welcome to the forums. Typically this forum is used for learning/ correcting purposes even on the more advanced stuff (which is in the 'Please read before posting' section of the forum rules) which means that forum members are not typically going to do the work for you. Instead, post up what you have accomplished so far and people will be MUCH more likely to help you.

hag++ 24 Junior Poster

Check out this snippet you have:

//Get the user to enter a sentence
	cout<<" Please enter a sentence.\n";
	cin>> Phrase;
            cout<<" The number of words in your phrase is "
          <<Phrase.length()<<".\n";

If you are ONLY looking for words, not the entire string length, then Phrase.length() won't work because it includes spaces. Pseudocode:
1) User enters string
2) Look for letters only which indicates program is in the middle of a word
3) If space is encountered, ignore it and consider one word in the string to be counted.
4) repeat until end of string

hag++ 24 Junior Poster

Ok.... adcodingmaster.... look at your first post, then your second post. The ONLY thing that changed was now you have colors for keywords....

Salem commented: And it still looks like crap! +19
hag++ 24 Junior Poster

rand() % 7 is going to get you numbers between 0 and 6 which will overstep the bounds of your array by 1 if a 6 is pulled.

I'm confused why you broke up the loop like you did?

That is correct, if you use rand() % 6 + 1 you will be good. Also, when you define a function:

void printScreen(char dice[16][6])

You only need the second array size defined.

void printScreen(char dice[][6])
hag++ 24 Junior Poster

I dunno if you read this already but this is probably one of the better explanations ive seen
http://compprog.wordpress.com/2007/11/20/the-fractional-knapsack-problem/

hag++ 24 Junior Poster

Try reading the rest of the replies in this thread and then figure out why you're not going to get it.

Lol... some ppl never learn

hag++ 24 Junior Poster

Code tags ppl!

hag++ 24 Junior Poster

Ok, sorry for beginner knowledge. :(

But could anyone post the whole source code?

short answer? NOPE! Remember, the whole point of posting on the forum is to LEARN. You study, post code, then if you have questions ppl on the forum will help you with the code you posted. Oh, and dont apologize for being new to programming. Nobody here knows everything there is to know about it. Im def no master at any programming language (yet.... hehe) and personally, I love answering questions and helping people when I can because when I was just starting out I also recieved A TON of help

hag++ 24 Junior Poster

That was the only one that I could really understand. I am taking this class online so we really have to read the book and teach ourselves. I have been reading so of the material from programming one to catch up. Sorry it probably was kinda lame. I will get better as I go one. I never give up. Thanks for your help and honesty. I really need it to get better. Thanks Walt.

Online prog classes?? Ouch! That is not such a great idea to be honest with you. A lot of classes are well suited for teach self/ online learning but I dont think programming is one of them. Having an instructor in front of you to give you visual examples and the biggest thing, the other students around you (asking questions and being totally lost with you hehe) is a BIG part of the learning experience...... just my opinion tho =) The second thing, I would have to agree with WaltP about your final code. I know you are just learning but now is the time to get it right because it will carry over to ALL your work in the future. The reason we don't like it is because it is not flexible at all. What if the user enters a string that is only 2 characters long? What if they enter 20?

Finally, this line in your code:

//Get the user to enter a sentence
	cout<<" Please enter a sentence.\n";
	cin>> Phrase;

Remember …

hag++ 24 Junior Poster

Hey robinotje, check out this msdn article. Read the top portion then scroll all the way to the bottom and check out the C++ code snippet.
http://msdn.microsoft.com/en-us/library/aa328747(VS.71).aspx

*EDIT: Keep in mind though, using mscorlib requires the use of .NET so you have to have all windows updates installed for the program to work. If you are using it within normal windows operation (i.e with the OS mounted and running, .NET installed) then the program should work fine. The program will not run in a PE enviroment UNLESS you create a winPE boot disk

hag++ 24 Junior Poster

no problem :P if you need any more help ask me i have a skype if you want to chat in real time about anything. or you can email me at [email]snipped[/email]

Sounds good :cool: Right back at ya

hag++ 24 Junior Poster

Thanks WaltP that second one i was just kind of assuming i knew it was a class but i didnt know it was an array managed by a class thanks again.
Which code anthony?

sorry hag++ it was originally a char array but i was trying to move it to a string.

Yea i know, the way both you and I had it before (i.e using char array) is quick and does the job for this project..... using strings is nice but not needed but somehow everyone started to veer towards using strings instead lol

hag++ 24 Junior Poster

jonsca couldnt he just do this

#include <iostream>
using namespace std;

int main()
{

      string input;
      int x;
      cout<<"input: ";
      cin>>input;
      for(x=input.length();x>0;x--)
      {
      cout<<input[x];
      }
      cin.get();
}

? yes i changed it around.

Is there a specific reason that you are using a string instead of char array? It would be easiest with an array because of how you can manipulate it

hag++ 24 Junior Poster

Dude, you've already been told about code tags and where to use them, AND where to find info about them on the forum. I cant help you if I cant read your code... i.e the messy brackets in your nested loops because of the lack of code tagging

hag++ 24 Junior Poster

the compiler gave me this error

error C2064: term does not evaluate to a function taking 1 arguments

What does that mean?

Whos code did you recieve that error with? my second example?

hag++ 24 Junior Poster

I understand the reason. Still it doesn't justify violating the rules.

As im def no master of C++ yet =) just wondering what issues you would run into with the loop? I know strcmp during the first iteration tests a value outside the bounds of the array but it is a read only operation... could this still cause some kind of problem?

hag++ 24 Junior Poster
char input[8];
cout<<"input: ";
cin>>input;
for(x=7;x!=0,x--)
{
cout<<input[x];
}

The above will work but it is limited becauseyou don't really know how large of a string the user will enter. Instead use this:

const int MAX_STR = 20;  // Obv can be any number
char input[MAX_STR];
cout<<"input: ";
cin>>input;
for(int x=strlen(input) - 1; x >= 0; x--)
	cout<<input[x];
hag++ 24 Junior Poster

This

int index = 0;
             while (strcmp(prog[index - 1], "-1") && index < NUM_PROG && cin >> prog[index++]);

looks really suspicious. At the first iteration you access prog[-1] , which invokes an UB. Not sure if it is a "real" reason.

The reason being is that you want strcmp to check prog at the last loop iteration before it cin's to prog[index++] to see if user added the sentinel value

hag++ 24 Junior Poster

Hey Nate, did you try running my code above to see if it worked for you? It works fine for me but its not working for iou.... see if you can catch something cuz im out of ideas

hag++ 24 Junior Poster

Another good way of doing it (without use eof() ) is:

while (myFile.getline(line, '\n'))
      cout << line << endl;
hag++ 24 Junior Poster

This is very weird.... what compiler are you using? It shouldn't make any diff in this case but im running out of ideas

hag++ 24 Junior Poster

I agree with Dave, depending on what you've learned so far, accept all input as a string and use atoi and atof string functions to convert them (So your prog wont freak out if letters are entered into an int or double variable)

hag++ 24 Junior Poster

Hmmm... thats exactly what I have and mine works lol..... you included the cstring library right? (im assuming you did or u should get a compiler error but thats really the only thing I could think of lol) If you type three inputs then -1, each one seperated by a space, then press enter it still waits for more input??

hag++ 24 Junior Poster

I ran the code to test it and just to let you know I forgot to add that the loop should terminate when a sentinel value is entered, that will work..... cept I cant edit my post anymore :( For example:

// Here, I use -1 as the sentinel value, so if the user enters -1, the loop will terminate
while (strcmp(prog[index - 1], "-1") && index < NUM_PROG && cin >> prog[index++]);
hag++ 24 Junior Poster
cin >> var1 >> var2 >> var3 >> var4;

cin will ignore the whitespaces and newlines and input to multiple variables

*EDIT: I noticed I didn't really offer a true solution because you dont know how many commands the user will enter so you could do this:

const int NUM_PROG = 10;   // Number of programs that could be run in single command
const int NAME_LEN = 80;  // Max program name size
char prog[NUM_PROG][NAME_LEN];
int index = 0;

while (index < NUM_PROG && cin >> prog[index]) index++;

This would input to variables until there is no more input and you can use the index to tell you how many programs the user entered.

hag++ 24 Junior Poster

*EDIT: Sorry, upon second look I see you return position and not index, so it would work, the second vers of the code is a little more concise/ clear tho and you dont need as many variables

hag++ 24 Junior Poster

Also, a smal logical problem :

int searchList(const int list[], int size, int value)
{
	int index = 0,
		position = -1;
	bool found = false;

	while(index < size && !found)
	{
		if(list[index] == value)
		{
			found = true;
			position = index;
		}
		index++;
	}
	return position;
}

The index will be incremented one more time even if the PIN is found because it runs one more time after the flag is set to true. If the PIN is located, you want to return that position. Like this:

int searchList(const int list[], int size, int value)
{
	int index = 0,

	while(index < size)
	{
		if(list[index] == value)
		return index;

		index++;
	}
	return -1; // Indicates PIN not found
}
hag++ 24 Junior Poster

I agree with jonsca, use a 2D array. The rest is all up to you and VERY customizable. Depending on how crazy your prof is.... you could step through the array and randomize each dice using rand() generator limited by the amount of letters in alphabet.

hag++ 24 Junior Poster

I dont know if Im totally blind (which is an option) but what problems are you having with the code. I was going to read through it and see if I could spot something but its easier if I know whats wrong.... guess I could also compile/ run it too

*EDIT: okay so I compliled your code, and the problem is in the insert money function:

while (more == 'y')
    {
    // This while loop will keep executing until the logic is false
    while (inMachine < sodaPrice)
    {
    // Outputing to console display and passing inputs to variables
    cout << "Please insert $" << sodaPrice << endl;
    

    // Subtracting the amount in the machine from the cost of the soda
    cout << "Amount Needed: $" << (sodaPrice - inMachine) << endl;
	cout << " ======================================= "  <<endl;
    cout << "Amount you wish to deposit: $" <<endl;
	cin >> newDep;
	cout << " ======================================= "  <<endl;
	cout << "You have deposited: $" << (inMachine + newDep) << endl;
    inMachine = (newDep + inMachine);
	}
}

You never update the loop variant for the top while loop. After internal while loop finishes, ask the user if they want to buy andother soda, and update the "more" variable. Another thing to be careful for is that your a re using all global variables. This makes debugging difficult and it increases the chances for logical errors.

hag++ 24 Junior Poster

yep, put thomas' and my posts together and you got yourself a correct answer haha

hag++ 24 Junior Poster

Looking at the for loop in your backwards function:

for(count = SIZE; count >= 0; count--);	{		cout << strPtr[count] << endl;	}

The first thing you print is strPtr[11] which will contain logical garbage, the array will only go to subscript [10] if the array size is 11.
Instead, what about this:

for (count = strlen(strPtr); count >= 0; count --)
                  cout << strPtr[count] << endl;