MyrtleTurtle 52 Junior Poster

Is it the boost library? Regex in particular? I have searched and searched, but this is all I could come up with.

If this is the correct library, then how do I get it to work in Dev-C++? I managed to add all the boost .h files to my Dev-C++ include files folder, so now it will compile when I #include a boost header file.

Does it even work in Dev-C++ though? I couldn't get it installed properly in Visual Studio. I think the path name was not quite right (to include the boost root directory).

Also, how do you use the regcomp() function? I've seen examples where it takes more than one argument, however in the regexp.h file (that came with boost) it lists only one argument. Here is the header file I am trying to use:

/*
 * Definitions etc. for regexp(3) routines.
 *
 * Caveat:  this is V8 regexp(3) [actually, a reimplementation thereof],
 * not the System V one.
 */
#ifndef REGEXP_DWA20011023_H
# define REGEXP_DWA20011023_H

#define NSUBEXP  10
typedef struct regexp {
	char *startp[NSUBEXP];
	char *endp[NSUBEXP];
	char regstart;		/* Internal use only. */
	char reganch;		/* Internal use only. */
	char *regmust;		/* Internal use only. */
	int regmlen;		/* Internal use only. */
	char program[1];	/* Unwarranted chumminess with compiler. */
} regexp;

regexp *regcomp( char *exp );
int regexec( regexp *prog, char *string );
void regerror( char *s );

/*
 * The first byte of the regexp internal "program" is actually …
MyrtleTurtle 52 Junior Poster

The error I get is 'address of while(AreThereMore) will always evaluate to true.'

You need the ()'s when calling the function.

Also I see a couple of things in your AreThereMore() function:

If answer is not true, you need to set it to false. And when you compare you need to check each answer. Instead of

if (reply == 'Y'||'y') //this will always evaluate to true, I believe...because it is asking[I] if reply == 'Y' [/I]OR [I]if 'y'[/I]
{ 
answer=true; 
cin.ignore();
} 
else(reply== 'N'||'n');
{

You can say:

if (reply == 'Y'|| reply =='y')
{ 
answer=true; 
cin.ignore();
} 
else if(reply== 'N'|| reply =='n')
//or just put the else, without a comparison:
else{
//
}
MyrtleTurtle 52 Junior Poster

It seems to work fine for me, too, though it took me a minute to figure out what the program does. The first number input is the size, right?

So if you input 4,3,2,1,0 it will say that the size is 4 and the elements are {3} 2} 1} 0}

It gets the highest number correctly for me, every time.

But it doesn't sort them. I can't see anywhere in your program where you have called your sort function, though, so maybe that's why it's not sorting?

MyrtleTurtle 52 Junior Poster

My compiler would not let me use sleep (with a small 's').
And...I always use larger numbers. I don't think your delay is very long. If you want a half-second delay:

Sleep(500);

You don't need the tab, if you want the stick to show up in the same spot each time (at the left). Or if you want it to show up in the middle of the screen somewhere, then put the tab after the return. Put one tab before the first line printed and omit the last tab.

If you want it to look like it's rotating, you'll need to add a horizontal line, won't you?

MyrtleTurtle 52 Junior Poster

Actually, I have never used while(!infile.eof()) { Sorry for giving not-so-great advice here. I haven't seen it used any other way though.

Using this syntax, when the while loop ends you can evaluate using eof().

How would you do that? With an if statement? while(infile >> input) { is a little confusing to me. What if you need to read things from the file into other variables besides input? Won't that mess it up? If not, why not?

Is there anything wrong with this: while(infile.good()) { ?

MyrtleTurtle 52 Junior Poster

EOF means end of file. You can check for it like this:

while(!infile.eof())

It looks to me like you have closed your file and then tried to read from it.

infile.close();	//Force closes the file

while (infile)
{
	if(
MyrtleTurtle 52 Junior Poster

I would use string grade; instead of char grade; , so that you can have the - or + with it. A char represents one character.

Also, why don't you use int main() instead of void main() ? And you probably want #include<iostream> instead of #include<iostream.h> .

MyrtleTurtle 52 Junior Poster

I kinda like unget(), just because I never used it before. It's like a new thing to play with..."hey, I didn't know you could do that!" kind of thing. lol

Here is what my instructor said:

// ... now if you loop up to the while at this point (your old code) 'ch' still has a digit in it from earlier and it kills the
// .... 'while(isalpha...' immediately and as a matter of fact you never get passed the \n or cr at end of line: infinite loop
// ... this is why you never read the next item
// ch = inFile.get(); ... this read get past the cr at the end of line just read.
// ch = inFile.get(); ... this read gets the first char of the next item name which you need as a prime for the big while loop above.

Still it would make more sense to me not to have to use inFile.get() twice in a row like that. I expect that y'all are correct in saying that it would be better to use getline() & then just parse the line.

Thanks for the advice.

MyrtleTurtle 52 Junior Poster

Ah...thank you, that works.

But the program still isn't reading more than just the first line. Any idea why?

char ch = inFile.get();
    int i = 0;
    while (inFile){ //while(inFile.good()){ = no change //while(!inFile.eof()){ = infinite loop
 
        //read a line...but we don't want the whole line, just the non-digit part

        while(isalpha(ch)|| ch ==' '){
            items[i].name += ch;
            ch = inFile.get();
        }

        inFile.unget(); //retrieve the last character read, which is the first digit 
        //add the rest of the items to the items array          
        inFile >> items[i].itemNo >> items[i].qty >> items[i].price >> items[i].safe;  
        i++;
        //TEST:
        cout<<"i: "<<i<<endl; //why does it stop after the 2nd iteration?
    }
MyrtleTurtle 52 Junior Poster

I can get it to work except that the first digit isn't read into the first numeric variable.

...and that it only reads the first line (sort of) correctly. It puts the first item into all of the spaces in items[].name, so that items[0].name = "Wong Batts"
items[1].name = "Wong Batts"
etc.

...and after the first line, qty, price and safe do not contain the correct amounts.

MyrtleTurtle 52 Junior Poster

My assignment this week deals with using a struct. I think I can do that part. What I'm having trouble with is reading the file into each variable.

The first item in the file is text with spaces. The next items are numeric and need to go into other variables, but the entire text (with spaces between words & not necessarily the spaces between the text & the first number) has to go into one string variable (for each line). Then the digits on that line are read into the rest of the variables.

I can get it to work except that the first digit isn't read into the first numeric variable.

Can you tell me a better (or the correct) way of doing this?

int i = 0;

    while (inFile.good() ){ 
        //read a line...but we don't want the whole line, just the non-digit part
        char ch = inFile.get();
        int j = 0;  //on each line... NO NO NO THIS IS WRONG!!!!

        while(isalpha(ch)|| ch ==' '){
            items[i].name[j] = ch;
            j++;
            ch = inFile.get();
        }

        //NOPE! Doesn't add 1st digit... because it's stuck in 'ch' b/c of inFile.get(); ?     
        inFile >> items[i].itemNo >> items[i].qty >> items[i].price >> items[i].safe;  
        i++; 
    }

//I tried these things & it did not work:
//        getline(inFile, items[i].name); //NO!!! Don't want the whole line, just the alpha part!
//        inFile >> items[i].name; // This only inputs ONE word, not the whole name

The contents of the text file:

Wong Batts 98723 243 …

MyrtleTurtle 52 Junior Poster

Here is one way of reading from a text file and displaying the contents:

string sentence;
    string readSentence;
    
    //open file to read
    ifstream inData;
    inData.open("YourFileName.txt");
    
    //read first line from the file
    getline(inData, readSentence);

    //show error if file not found
    if(!inData){
          cout<< "Can't open input file.\n";
          getchar();
    }
    
    //while there are more lines to read from file
    while(inData){     
          sentence = readSentence; 

          //write data to screen     
          cout << sentence << endl;
          
          //read another line
          getline(inData, readSentence);
    }
    
    //close file
    inData.close();
MyrtleTurtle 52 Junior Poster

Well, just look where you put the brackets and what is inside them. What does return 0; do? Do you want that inside your while loop?
Also, I noticed before, in your other question, that these brackets don't seem to belong here. And if it was me, I wouldn't indent quite so far.

{
              cout << "\n**Played a cool game**";
              cout << "\nDo you want to play again? (y/n): ";
              cin >> again;
}
MyrtleTurtle 52 Junior Poster

i++;
j--;

If j==5 and i==0
and we increment i at the same time as we decrement j...

after the 1st iteration:
i = 1
j = 4

2nd iteration:
i = 2
j = 3

3rd iteration:
i = 3
j = 2

And your loop doesn't stop until i and j are equal...do you see a problem here? I did not compile it, so you may have other issues as well, but that's the main one I see.

edit: Aia and Ishara both beat me while I was typing. //hard to type when someone's beating you, that's why I'm so slow...lol

MyrtleTurtle 52 Junior Poster

Yes, depending on what you want to do. But in this case you really don't even need the if statement, since the loop will only stop whenever f==5. So we already know that it's 5, and don't need to check.

I would think that this logic is always valid whenever you have a while loop that ends when a certain value is reached and then you want to check for that value after exiting the loop.

But if you want to check for a certain value before entering the while loop, then you'd put the if statement first.

MyrtleTurtle 52 Junior Poster

Not trying to butt in here, but I'd say that the error is on (or at least caused by) a line above the underlined one.

A while loop should include brackets like:

while(condition){//bracket goes here
     //do stuff
    
}//end loop
MyrtleTurtle 52 Junior Poster

...maybe this should be in another forum?

MyrtleTurtle 52 Junior Poster

Or instead of a sort, I know this will work (because I just wrote a program to do this).

Each time the user inputs another value for your variable, check to see if it's <= each of the other variables. If so, increment that variable by one.

cin>>c;

if(c<=a){
    a++;
}
if(c<=b){
    b++;
}

Then when you are done, they will already be in order.

You can use an array of chars or strings, and assign the values like:

ary[a] = 'A';
ary[b] = 'B';

etc.

There's probably a better way of solving this, but oh well. This works.

MyrtleTurtle 52 Junior Poster

You're trying to calculate before you get the input. This line should go after you get the input to assign a value to price and quantity.

total = price * quantity * tax;
MyrtleTurtle 52 Junior Poster

It is because each line executes in order. The order you have here is to check if f == 5 first, and if so then cout<< your message. If f!=5 though, it will check to see if f==5 once, then go into the loop. There is no code to take you back to the check to see if f==5 and cout<< your message after you exit the loop (when f==5).

if (f==5)	cout << "Well done!\a\a\a";	while (f!=5)	{	cout << "Wrong answer, try again!\nEnter the number here: ";	cin >> f;	}

This one keeps asking for input as long as f!=5. Then, whenever f==5, it will exit the loop and go on to your if statement to check the value of f.

cin >>f;
	
	while (f!=5)
	{
	cout << "Wrong answer, try again!\nEnter the number here: ";
	cin >> f;
	}
	if (f==5)
	cout << "Well done!\a\a\a";

edit: Oops, Walt, you beat me to it. I finally found a question I can answer...so I'll leave my answer here even though it's the same thing you said.

MyrtleTurtle 52 Junior Poster

Correct me if I am wrong, but it looks to me like what you would like to do is something like this. The inputs are labeled A, B, C, D, and E, respectively. So your first input is the rank of where A is, out of the total amount. So if you put in 1/1, that means that A comes 1st out of 1. Your next input will be for B's rank. B is first now, so we put in 1/2, 1st out of 2 total (A and B). When you input C's rank, it will be second out of the total number of ranks input so far, which is 2/3.

Is that right? If so, then I don't think you even need to put the / and the 2nd number, just keep a count of the total. You could start with your 5 variables and input their ranks in order, then figure out which order to display them in. A sort would work, I think.

MyrtleTurtle 52 Junior Poster

It worked when I did it just now:

if (action == 1){
               system("cls");
               cout << "then have this show up but none of the above text ^^." << endl;
               }

According to this site you can also use clrscr(); if you have Borland C++ Builder.

MyrtleTurtle 52 Junior Poster

some people use

system("cls");

Here's one example.

MyrtleTurtle 52 Junior Poster

I would make a more general function instead of telling it the exact number of lines.

Here's a page with an example of how to check for end of file.

MyrtleTurtle 52 Junior Poster

Your program compiled for me (Dev-C++) but froze after printing the total sales.
I'm not sure about error LNK2019 (it didn't give me that error), but you can solve the other one by initializing your variables :

int totalJarsSold=0;
//do the same for the rest of them

Also, this looks like a 2-D array to me. It seems to work, though I guess you could use a string type? I don't know, maybe it's just a matter of preference?

char name[NUM_TYPES] [SIZE] = {"mild", "medium", "sweet", "hot", "zesty"};

//or
string name[NUM_TYPES] = {"mild", "medium", "sweet", "hot", "zesty"};
MyrtleTurtle 52 Junior Poster

I do not know how to add new columns to a matrix either.

This may not be the exact answer you are looking for, but I think it is similar to what you want to do:

//if you had a vector called nums, and a 2d array called ary, you could do this:
//put nums from vector (or you could use an array) into 2d array
  for(int r = 0; r<ROWS; r++){
        for(int c = 0; c<COLS; c++){
                ary[r][0]= nums[r]; //for every row, the 1st column will be populated from the vector...no change to the other columns
        }
 }
MyrtleTurtle 52 Junior Poster

What does the program do as opposed to what it's supposed to do?

Are you getting any errors?

Also you should probably use code tags: paste your code between tags like these, without spaces [/code ].[code ]paste your code between tags like these, without spaces [/code ].

MyrtleTurtle 52 Junior Poster

You also still have the same 2 issues I mentioned the other day, in your other thread about the 2-d arrays (with this program).

While I do not think this will affect your program (because you get the value with user input later) it is still not correct.

char Y = Y; //this does not initialize it to the character Y. You need 's like char Y = 'Y';
   cout<<endl<<Y<<endl; //add this code & see what it prints

And you have that infinite loop again, because you don't provide a condition for which the loop can be stopped. It just keeps asking for scores, forever.

do{
      //your code here
             
      getUserInput(Y);   //add this to fix endless loop     
  }while ((Y == 'Y' || Y == 'y'));
MyrtleTurtle 52 Junior Poster

score is declared local to main() and you are trying to use it locally to printplayerinfo(). This is not possible without passing it to the function.

True, but it has been redeclared within the function:

void printplayerinfo (const int& numofgamesinseries, const int& numofplayersonteam, int& i, int& j)
{
int score[numofgamesinseries][numofplayersonteam];
MyrtleTurtle 52 Junior Poster

In addition to what Fbody said, you have an infinite loop.

char Y = 'Y'; //add 's around your char

printintromessage ();

getUserInput (Y);
do
{
if (Y == 'Y' || Y == 'y')

{
printplayerinfo (numofgamesinseries, numofplayersonteam, i, j);
//you'll have an infinite loop unless you stop it somehow
getUserInput (Y);
}

Also, I have better luck when I start for loops at zero:

for (i = 0; i < numofgamesinseries; i++)
{
for (j = 0; j < numofplayersonteam; j++)
{
MyrtleTurtle 52 Junior Poster

I suspect you will have several more errors show up.

You were right. I did what you advised, and everything you said was correct. Thank you for the help.

It did not work for me the first time I tried your advice because I only changed the ONE prototype. But when I changed them all, it did.

MyrtleTurtle 52 Junior Poster

I appreciate your trying to help, but like I said, this works:

int main(){

    system("color e1"); //black & white all the time is kinda boring...
    ofstream outFile;
    outFile.open("Harvey.txt");
    
    //counters 
    int water = 0, safe = 0, flower = 0;
    
    int ary[ROWS][COLS];
    
    populateAry(ary);
    printAry(ary, outFile); //just to make sure the array has been populated correctly
    
    //attempt to walk across the island--100 times
        for(int i = 0; i < 100; i++){
        //reset field of flowers for each attempt
        populateAry(ary); 

        int row = 6, col = 1;  //reset position
        int position = ary[row][col]; //start Harvey out with his first step, forward (to my right), for each attempt
      
       //this loop represents each attempt to walk across the island.
        while(position != 3 && position != -1){
            position = walk(ary, row, col, position);       
        
            //check position & update status
            update(ary, row, col, position, flower, water, safe);
        }
    }//end for loop

    double avg = calcCost(flower);

    printResults(water, safe, flower, avg, outFile);
    
    outFile.close();

    cin.get(); 
    return 0;
}

All of the above function prototypes and definitions are unchanged. The only thing that is different is the runSim function is omitted, and the code that was in it is in main instead. I just can't figure out why it works for every other function except for that one.

And what I meant was that the prototypes do not even need a variable name at all. They only require the data type, which is the way I have it. In fact, I usually omit prototypes altogether, but my instructor wants them …

MyrtleTurtle 52 Junior Poster

Thanks for the reply. That didn't work though. In my experience, the prototype doesn't actually need to include the variable names, just the data types.

The program does work with all the other functions, just not with that one function...

MyrtleTurtle 52 Junior Poster

This is my first question here.

My latest assignment is to create a random walk program using a 2-d array. The program worked fine until I tried to put this code into a function:

for(int i = 0; i < 100; i++){
        //reset field of flowers for each attempt
        populateAry(ary);  

        int row = 6, col = 1;  //reset position
        int position = ary[row][col]; //start Harvey out with his first step, forward (to my right), for each attempt
      
       //this loop represents each attempt to walk across the island.
        while(position != 3 && position != -1){
            position = walk(ary, row, col, position);       
        
            //check position & update status
            update(ary, row, col, position, flower, water, safe);
        }
    }//end for loop

It works fine when I leave that code in main(). The other functions work! But when I try to do the same with this code using a function (just like the others?), I get these errors:

invalid conversion from `int (*)[14]' to `int'
initializing argument 1 of `int walk(int, int&, int&, int)'

invalid conversion from `int (*)[14]' to `int'
initializing argument 1 of `void update(int, int&, int&, int, int&, int&, int&)'


This is the function (prototype & definition):

void runSim(int, int, int, int);

void runSim(int ary[ROWS][COLS], int flower, int water, int safe){
    
    for(int i = 0; i < 100; i++){
        //reset field of flowers for each attempt

        populateAry(ary);  //I find it ODD that the compiler does not complain about this one!?!

        int row = …
MyrtleTurtle 52 Junior Poster

So that's what that is for. lol

MyrtleTurtle 52 Junior Poster

Did you want to call this function here? Can't do that.

outfile <<"The area of the given "<<displayShapes;
outfile <<"The area of the given "; //print stuff
            displayShapes(outfile, ashape);  //call function...cannot call it with outfile
            outfile <<" is "<<Area;  //print more stuff

My output after making those changes:

The area of the given Triangle is 210
The area of the given Rectangle is 200
The area of the given Circle is 314
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400
The area of the given Square is 400

MyrtleTurtle 52 Junior Poster

:ooh: Oh, you just want to display it all on one line. Then you don't need to worry about how you get them, just how you display them.

When you cout<< the numbers, just don't print any spaces or endl (or "\n").

But...I don't think you can do this part all on one line, if that's what you mean:

5 (press enter) 1(press enter) 2(press enter)

That is what the 'enter' key does, after all...goes to a new line.

It pops up the error 'i redefinition'.

I would guess that is because of the second for loop, where you have 'int i' again. Just omit the int part. That code works fine for me as is, though.

MyrtleTurtle 52 Junior Poster

I didn't say you can't use a switch.

//declare function as void
void aFunction(pass your variables here){

    //put your switch in here
	switch( ashape )
	{
		case Triangle: outfile<< "Triangle" ; break;
		case Circle: outfile << "Circle" ; break;
		case Rectangle: outfile << "Rectangle" ; break;
		case Square: outfile << "Square" ; break;
	}
	cout << endl << endl;//probably want outfile instead of cout, don't you?

}
MyrtleTurtle 52 Junior Poster

im trying to call the functions above int main() but my compiler is saying that i am redeclaring them.

You declare functions outside of int main() but you call them within int main(). So if you're trying to call them outside of the main() function, then it (the compiler, I mean) would think you're trying to declare them again.

Also, it is much easier to see what the problem is if you post your code.

MyrtleTurtle 52 Junior Poster

If all your function does is display (print) the name of the shape, then why return anything? I'd just call it a void function and omit the return statement.

Then when you call it, don't assign it to anything:

aFunction(); //call a void function

//declare function as void
void aFunction(){
    //print stuff
}

//unless you're using recursion, I don't think you want to call your function within itself. But if you do call it, you'll want to include the ()'s

MyrtleTurtle 52 Junior Poster

I've seen posts here that were posted in the wrong forum. Today there was one in the IT professionals forum that should be posted under HTML instead, and one with C code in the C++ forum.

What do you think about creating a forum for members to report posts? Besides 'wrong forum' posts, I'm sure you have people posting other 'undesirable' content here (spam, etc). Why not have a way for members to report it, so it gets taken care of quickly?

Not to say that your moderators aren't doing a fine job already, but I have seen this done on another rather large website, and it helped the moderators quite a bit. After all, you can't be everywhere at once, can you?

:)

MyrtleTurtle 52 Junior Poster

do people jump down your throat

Not everyone. Mostly it's just one persistent mod.
lol

You should be able to write simple programs, and probably make errors while doing it.

I have that second part down pat.

Thank you both for the replies.

MyrtleTurtle 52 Junior Poster

As I said...

Well, shoot. You're right.

(again) :icon_redface:

WaltP commented: My comment was pointed at Vernon -- he said exactly what I said. You were agreeing. ;o) +11
MyrtleTurtle 52 Junior Poster

That doesn't enter 5 numbers. It enters 5 characters.

Well, shoot. You're right.

But, you can convert a character to a digit easily enough. And with Vernon's code you still have to press enter after each number is entered. Though I do like the idea of a for loop and an array rather than my repetitious code.

MyrtleTurtle 52 Junior Poster

Do you mean that you don't want to have to press enter after each (one character) number is entered? If so, then yes it is possible. I'm sure I have seen this done a different way, but here is one way anyhow:

char num1, num2, num3, num4, num5;
    cout << "enter 5 numbers:";
    num1 = cin.get();
    num2 = cin.get();
    num3 = cin.get();
    num4 = cin.get();
    num5 = cin.get();
 
    cout<<endl<<num1<<endl<<num2<<endl<<num3<<endl<<num4<<endl<<num5<<"\nPretty cool huh!\n";

You'll still have to press enter after the 5 numbers are entered, and this doesn't have error checking, of course. BUT if you enter 54321 (enter) then you will have your desired output. You could also enter a 5-letter word if you wanted to, since cin.get() gets one character.

MyrtleTurtle 52 Junior Poster

Could you use a while loop? Here is an example of a while loop that runs until a condition is met:

bool x=false; 
int num = 0;     
while(!x){//while x is false
    //test
    if(num ==5){
    //if pass:
         cout<<"true";
         x = true;
    }
    else{ //if fail:
        cout<<num<<endl;
        num++;
    }
    //should exit when num is 5
}
MyrtleTurtle 52 Junior Poster

I did it like this:

//separate each digit & place in array
   nums[0] = number/1000; //first digit
   nums[1] = (number%1000)/100; //second digit
   nums[2] = (number%100)/10; //third digit
   nums[3] = number%10; //fourth digit
MyrtleTurtle 52 Junior Poster

You don't even need to pass in grade or tot_quest into your functions if you are getting their values within the function. You can just define them within the function (if you don't need them anywhere else in the program.)

double calculateGrade(double grade)
{
    int quest_corr, tot_quest;
    grade = quest_corr/tot_quest;
    cout << grade;
return grade;
}

This will not give you the correct answer because you define quest_corr and tot_quest here, but do not give them a value. Then you use them in a calculation. I would define the variables, get the values and then pass them in, or get the values within the function as you do with tot_quest in your other function.

MyrtleTurtle 52 Junior Poster

You can write nested loops like this. Just put a loop inside of a loop:

for(int x=0 ; x<=10; x++){      
    //outer loop runs last
    for(int y=1; y<=10; y++){ 
        //this loop runs second
        for(int z=0; z<=10;z++){
            //inner loop runs 1st 
        }//end for z 
    }//end for y
}//end for x
MyrtleTurtle 52 Junior Poster

I think that they are similar, but IMHO it would be easier to learn one language first, get the basics down, and then concentrate on learning others. In my experience, quite a few of the more popular languages are similar, especially the basic concepts. It's mostly just the syntax that differs.

Ancient Dragon commented: Agree. +27