Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Your error check is not going to work. Why? Because if someone enters more than 16 characters then the entire program will get trashed and there is nothing that you can do to avoid it. That's one of the problems with scanf().

What I would do is get the string one character at a time and produce an error on the first character that would overflow the destination character array.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

What is the effect of the following code?
main()
{
int option=4;
switch(option/2)
{
case 1: printf("EAST");
case 2: printf("WEST");
case 3: printf("NORTH");
case 4: printf("SOUTH");
}
}

Hint: there are no break statements between the cases.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You are asking about how to flush and discard the input stream of all its contents. Narue wrote this thread some time ago that explains how to do that.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Please post your question(s) here. I will not respond to PM.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Number of iterations? Infinite. Why? because 1/2 = 0 (integer division) so the value of x will never change.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Oh I see. You need to make it like this: void print_hello(int n, int ch); and use the variable ch instead of hard-coded stars. That would make the print statement in my previous post like this: printf("%c%cHello World%c%c", ch,ch,ch,ch); where each %c tells printf() to get the character from the parameter.

You also need to change the function prototype at the beginning of your program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

jephthah, as new member of the forum I'm just trying to fit in and try and not upset all the people who have been here for a long time so apologies for replying to an 11 day old thread - at least I now know that even if I can help someone I shouldn't reply to any threads older than 10 or 11 days.

You normally will get bad rep if you bump a thread that has been dead for 6 months or longer. 10 or 11 days is ok. We've seen some people bump 5-year-old threads, most likely because they didn't pay attention to the date of the last post.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Now if only the OP will understand the significance of your post and how easy it will make his program's solution.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

case statements must be constants. You can not use variables there

switch( some_variable )
{
case 'a':
  // blabla
  break;
case 'b':
   // blabla
   break;
case 'c': case 'd': case 'e':
   // this for all three characters
   break;
<etc. etc for each letter you want to support>
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>void print_hello(int n, int *)
What is that second parameter for?

All that is doing is printing one line of * below the text. What about above the text and on each side?

****************
** Hello World ***
****************

This is how to do it

  1. Print the top line
  2. Print two *, then the text, then the last two stars, such as printf("**HelloWorld**\n");
  3. Print the last line

You can not do that all in one big loop.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ok, sorry I misunderstood. I won't be able to look at it again for the next 36 hours (sleep + work + honey-do-list).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

One of the problems is that when the current value (509) is less than the value at the root (913) there is no provision to insert the new value at the root of the list. Instead, it inserts it at t->left, but it should be added at t. Learning to format your program better to make it more readable might help you understand what is going on

BinaryNode *BinarySearchTree::insert(int x,
BinaryNode *t) 
{
    static long count=0;
    count++;
    if (t == NULL)
    { 
        t = new BinaryNode(x,count);
        count=0;
    }
// t->key == 913 and x = 509
    else if (x < t->key)
    {
      // this is wrong when x < t->key
	    t->left = insert(x, t->left);

    }
    else if (x > t->key)
    {
	
        t->right = insert(x, t->right);

    }
    else
        throw DuplicateItem();
    return t;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int count[101] = { 0 }; //assign all elements to zero value

if (your char is a number from 1 to 100)
  count[char + '0']++;

//remember count[0] will not be used here.

Your example is just a little bit wrong. The file contains integers, so if they are read as ints then no conversion is necessary

int count[101] = { 0 }; //assign all elements to zero value

if (the value of the integer is a number from 1 to 100)
  count[number]++;

//remember count[0] could be used here if the value of the
// integer in the file were 0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to add a constructor to Rectangle that takes no parameters -- that's called the default constructor.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

using vc++ 2008 express I get a lot of these warnings
warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)

The Parser.h you include in main.cpp -- it that one of your header files? There is also one in the Windows SDK, which is the one that vc++ 2008 express is picking up.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>but don't kn0w how t0 apply it for 3 c0nditions ?

Is the o key broken on your keyboard? if( a > b) ? a : if( b > c) ? b : c; You could in theory just keep going on an on like that, but it will just make the code nearly impossible to understand.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to also post RuntimeException.h and its corresponding *.cpp file if there is one.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Post entire code so that we can compile and test it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>static long count;
>>count++;

Your program is incrementing an uninitialized integer and then you use that integer later on in the function. initialize it to 0 static long count = 0;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you used c++ <vector> class you would not have to worry about it because that class will do all the expanding for you. But if this is homework then you may or may not have that choice.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I found setjmp and longjmp quite useful in database programs to quickly return errors between deeply nested function calls. c++ exceptions are the alternative in c++ programs. goto is only useful within the same function. setjmp and longjmp are between functions, although could be used within the same function too.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>It appears to me almost that a BSTR could be used very similiarly to a VARIANT in Visual Basic

Not really. C also has a VARIANT structure which, among other things, contains a BSTR. The VARIANT is an enumeration of a lot of different pointers and data types. In C its main purpose is for COM, but can be useful to VB as well. When VB passes a string to C it is usually BSTR.

>>and the SysStringLen() is 8!!!!

Why? Should be obvious is you look for that function in MSDN. That function expects the BSTR to contain a UNICODE string (wchar_t*), which in your case it does not (char *). As I said previously, you, the programmer, are responsible for the contents of BSTR, and its up to you to use the appropriate win32 api functions to deal with it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think it would be a lot simpler to use getline() to read an entire line, then parse its contents. With this you don't have to worry about unget().

string line;
while( getline(infile, line) )
{
    // parse the string here, similar to the way you did
    // it in your original program.

}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There are several ways to solve that problem. IMHO this is the simplest.
sprintf() is your friend here

char filename[255]; // final file name
 char* namePt1 = "Labyrinth";
int num = 5;
sprintf(filename,"%s%d.txt", namePt1,num);

ofstream fileLabyrinth(filename);

Alternate method is pure c++

string filename;
string namePt1 = "Labyrinth";
int num = 5;
filename = namePt1;
stringstream str;
str << num;
filename += str.str();
filename += ".txt";

ofstream fileLabyrinth(filename.c_str());
DSalas91 commented: He help me very well! :) +0
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The four bytes before the beginning of the BSTR is the number of bytes allocated to the BSTR, which may or may not be the length of the string. In fact the BSTR need not contain a null-terminated string at all (either UNICODE or ascii). It could contain binary data of any type. It's up to your program to interpret the contents of the BSTR correctly.

In the example code you posted, the value of 34 is correct because 17 * sizeof(wchar_t) is 17 * 2 = 34. Note that in this case the string is NOT null terminated because you didn't tell SysAllocStringLen() to include that. If you wanted the null terminator then you have to add 1 to the length of the string.

Run the same program on *nix and you will probably find different results because sizeof(wchar_t) on MS-Windows (2) is different then it is on *nix (4). So if you transmit that BSTR from MS-Windows to *nix (or vice versa) then some sort of translation will need to be made.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The functions isalpha() and isupper() will tell you whether the character is an upper-case character or not

for(int i = 0; i < userInput.length(); i++)
{
    if( isalpha(userInput[i])  && isupper(userInput[i]) )
        count++;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> //NOPE! Doesn't add 1st digit... because it's stuck in 'ch' b/c of inFile.get();

call inFile.unget() to put the digit back onto the stream.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Depends on the file's contents. As a general rule of thumb, just open the file as a normal text file and read its contents one line at a time. Beyond that, you will have to tell us what's in the file.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I like the second example better, but delete line 14. Of course it won't work if you need to know the value of m after the loop terminates.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A little more testing indicates that in the evaluate() function, when you find a digit it needs to convert everything up to the first non-digit to an integer and then push that result onto the stack. For example if I enter "10+20+30" the function should push the values 10, 20 and 30 onto the stack. What it is now doing is pushing the individual digits, 1, 0, 2, 0, 3, 0. You could fix that something like this:

for(i=0;pos[i]!='\0';i++)
    {
        if(isdigit(pos[i]))
        {
            int x = 0;
            while( pos[i] != '\0' && isdigit(pos[i]) )
            {
                x = (x * 10) + pos[i] - '0';
                i++;
            }
            push(&eval,x);/*use to find the integer value of it*/
        }

But that too only works for values less than 126 because you are using char as the data type of the stack. Use long long and you could get much larger values and would require some additional recoding.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem is in the evaluate() function. The content of the parameter contains spaces, which should not be evaluated in that function.

if(isdigit(pos[i]))
		{
			push(&eval,pos[i]-'0');/*use to find the integer value of it*/
		}
		else if( !isspace(pos[i]) )
		{
			p2=pop(&eval);
			p1=pop(&eval);
			result=operation(p1,p2,pos[i]);
			push(&eval,result);
		}

That gets rid of the program crash, but it still doesn't work when I enter a little more complex expression.

Enter Infix Expression : 1+2+10+234


Postfix Expression is : 1 2 + 10 + 234 +
7
Press any key to continue . . .
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped

That tells you that it did not generate an executable program. Did you save the source file after you created it?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

No one knows everything about everything. IMO you will know you are a good programmer when your peers tell you that you are one. And that takes a lot of time and practice. Employers do not expect entry-level people to know a lot about programming. You should be able to write simple programs, and probably make errors while doing it. You will not be expected to have the knowledge of someone with a Ph.D. or 10 year's experience. So don't be so hard on yourself. I had a prof who told the class once that graduating from college is not the end of your learning experience, but only the beginning. College just gives you the tools to learn.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Probably not. I suppose you could keep it encrypted, but even that will not prevent some smart hacker from decrypting the password. There really is no bullet-proof way to save passwords, anywhere except in the human mind.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes, bubblesort() still has two more problems

First, the sort algorithm doesn't work right.

for(x = 0; x < i-1; x++)
      for(y = x+1; y < i; y++){
            compares(1); //1 comparsion made
            if(a[x] > a[y]){
                  temp = a[x];
                  a[x] = a[y];
                  a[y] = temp;
                  moves(2);
            }// end of if
      }//end of for

second, the loop is incorrect when displaying the array for(j=0;j<=i;j++){ that should be j < i not j <= i

churni commented: thanks for the help - you really are a genius in my eyes! +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 61: the value of variable i has not been initialized. As Aia already mentioned, get rid of the i parameter in the three functions that are called from main() with it. Just declare the variable local to each of those functions.

I see that problem on the very first attempt. I asked it to generate 100 numbers and it said that it generated 101.

[edit]Ok, your program works ok, it's just that you are printing the wrong numnber

printf("%d floats merge sorted in %ld moves using %ld comparisons\n"
		                      ,i+1,moves(0),compares(0));

That i+1 should only be i because the value of i already contains the number of items in the array.
[/edit]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you will have to post the entire program because there is no way anyone can determine the problem from the little code you posted.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

post current code

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Is there a way to fix this?

Probably. My guess is that the program is not using the right brush color for painting. There are lots of ways to do that but the simplest might be to make the brush a global object so that it doesn't get destroyed between paint function calls.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

On my Code::Blocks, F9 toggles a breakpoint. But to compile/link and run the program, you probably want to use F12. See the Code::Blocks' Build menu.

You may have an old version of the IDE. I have 8.02 and F9 is "build and run"

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to be careful of lines like this: while(myStack->data[top]!='(')

top is not a variable, but a member of the STACK structure. So you need to write it like this: myStack->top. You have that same error several times.

>>push(&myStack, symbol);
variable myStack is already a pointer, so remove the & address operator.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Did you create a console project? e.g. File --> new --> project then select Console Application in the popup box.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

post the whole program. In the code you posted the variable hydraulicRadius was not defined. Did you include <math.h> ?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You don't really need an iterator -- you can just index std::string just like you did the char arrays.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

At this moment I need to figure out how to read in a char array including the white spaces cause cin get() and even getline() all delimit at the whitespace when it comes to the gap on the answer

what?? I think you may be confused. getline() ignores spaces and stops reading then either '\n' is encountered or the input buffer fills up (assuming you are using character array).

is it possible to use a cast??

No.

maybe to take it in like a string but store in a char array cause the ifstream wont like me input like a string

This might be a lot easier if you would use std::string instead of character arrays

std::string answers;
std::string key;
std::string line;
ifstream in("datafile.txt");
getline(in, key);
while( getline(in, line) )
{
    answers = line.substr(0,15);
    name = line.substr(15);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Just for the book i am applying a ton of compression techniques but lets just say for some reason we still can't be able to fit the entire structure onto main memory what would you do?

you might have at least a couple choices

  1. Use a 64-bit compiler which gives the program more memory. But that's won't help if the program already maxed out the computer's RAM.
  2. Swap the data to disk. This is a bad solution because it will be very very sloooooow.
  3. Replace the array with an SQL database table.
  4. Re-design the program so that it's not such a memory hog.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use getline() instead of >> so that the entire line is read into a single variable.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I havent put the other code in for the other loops but i need to be able to count the amount that were correct wrong or missing

Well then just do it in one loop. Try to use as few loops as possible because they are very time expensive.

if( answer[i] == ' ')
   missing++;
else if( answer[i] == key[i] )
   correct++;
else
   wrong++;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You don't need to know whether answer is 'T' or 'F'. Just test if( answer[i] == key[i]) You can then delete one of those two loops.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>inQuiz.seekg(0);//set position to begining of file

Delete that line. When the file is opened the file pointer will already be set to the beginning of the file.

>>inQuiz.seekg(1 - 1, ios::cur);
Why? what purpose does that serve?