MyrtleTurtle 52 Junior Poster
if (ashape = Triangle)
	{
		Area = .5 * d1 * d2;
		return Area;
	}
	else if (ashape = Circle)
	{
		Area = 3.14 * d2 * d2;
		return Area;
	}
	else if (ashape = Rectangle)
	{
		Area = d2 * d3;
		return Area;
	}
	else if (ashape = Square)

Two equals signs are needed to test for equality. One equals is the assignment operator, so you're setting the values each time, and (if I am not mistaken) the if statement will always return true in that case.

Also you're comparing a char to a variable of type 'Shapes'. Is that a char as well? If not, I don't think it will work.

char ashape;

	if (ashape == Triangle)

Also, about the indenting, what I took it to mean was that you have some unnecessary indenting, although indenting is the right thing to do. Oddly enough, that was one of the hardest things for me to get right, when I started programming.

}
		ofstream outfile ("PJ657_output.txt");  // why indent this?
			if (!outfile)
				{  // why indent this?

Maybe something like this would be better/more readable?

} ofstream outfile ("PJ657_output.txt"); 
		if (!outfile) {
MyrtleTurtle 52 Junior Poster

Forgive me if this is not the correct place to post this question.

How do you judge your own programming skills? How do you know you're 'good enough' to land a job in the field and to do a good job?

There are so many more questions that I cannot answer than ones I can, which is a little discouraging.

What gives you the confidence to know that you are a good programmer?

MyrtleTurtle 52 Junior Poster

Mixing cin with getchar() should not be done.

Okay. But what if you want to use cin for your program, but then you want to pause the screen without using

system("pause");

?

I expect you'd use cin.get() ? Is it better to do that or use getchar? Or does it depend on the program?

Apparently you still have to clear the buffer (or get it to ignore whatever's in there) in order for cin.get() to actually pause the screen.

MyrtleTurtle 52 Junior Poster

I might be using anything...but I am using Dev-C++ for now. I also have VS C++ 2008 Express, but haven't used it much yet.

I think I know what my problem is with getchar. What do you think?

char digit=0;
    int digit2 =0;
    cout<<"Enter a digit: ";
    digit = cin.get(); 
    
    cout<<"Enter 2nd digit: ";
    cin>>digit2; 
    
    cout<<endl<<digit<<endl<<digit2<<endl;
    
    cout<<"Press any key:\n "; 
    digit = getchar();  //compiler skips right over this
   // cout<<digit<<endl;
    digit = getchar();//this one works, only after it skips the 1st one...something left in the buffer maybe? like the endline character? or whatever is left from pressing enter after entering the digits above?
    cout<<digit<<endl;
    cout<<"Press any key:\n "; 
    digit = getch();  
    cout<<"Digit: "<<digit<<endl; //this works
MyrtleTurtle 52 Junior Poster

edit: @Myrtle, just cin.get() or even getchar() instead of the decrepit getch().

yes, I have heard this before...but getchar(); NEVER works for me! It always just skips right over it, as if getchar(); is not even in my code.

MyrtleTurtle 52 Junior Poster

There are several ways of doing what you want to do here. I'm sure that I have omitted some, but these will work:

char digit=0;
    int digit2 =0;
    cout<<"Enter a digit: ";
    digit = cin.get(); //assigns whatever (single) character the user enters to variable 'digit'
    //if they enter more characters it will not use them for this variable but keep them in the buffer
    //and act as if the user entered whatever's in the buffer, the next time it wants input
    
    cout<<"Enter 2nd digit: ";
    cin>>digit2; //assigns whatever (int) number the user enters to variable 'digit2'. Doesn't [I]have[/I] to be an int, but that's how I declared digit2 so that's what this one will be
    
    cout<<endl<<digit<<endl<<digit2<<endl;
    
    cout<<"Press any key: "; //this may be the one you're looking for, but...
    getch();  //need to #include <conio.h>, though I've been told it's not a good thing to do...but it works
MyrtleTurtle 52 Junior Poster

lol, this is how American I am. I did not even realize that Queen Elizabeth II is the name of the current queen. (thanks, Google)

But Google didn't help me find whether it is true that she's the one who officially declared the US independent. The closest thing I could find was this, and according to the last line in that article, the US was officially acknowledged as independent from Great Britain (by them) in 1783.

MyrtleTurtle 52 Junior Poster

BTW it was Queen Elizabeth II who formally declared the USA an independent country. I don't recall when it was but I do remember seeing it on tv.

Must not have been too long ago, if they had tv at the time. Either that or you (and tv) are older than I thought.

MyrtleTurtle 52 Junior Poster

here we do not not give programs to people,

Ah, so you do give programs to people?

//just kidding...double negative there, lol

WaltP commented: oops.... that was not not right, was it it? +11
MyrtleTurtle 52 Junior Poster

It was adding a way huge number at the end, which made your calculations off.

Since I cannot edit the post, I'll just add another reply here.

I think the reason for the way huge number is because your array is un-initialized. In case anyone (who didn't know already) wants to know. :)

MyrtleTurtle 52 Junior Poster

After compiling your code using my own input file I see the problem. You were adding the sum in the wrong place, which was after the last (unsuccessful) read from the file. It was adding a way huge number at the end, which made your calculations off.

I put a couple extra cout's in there to test. Remove them and this code should be what you want:

i = 0;
    fileIn >> scores[i];
    cout<<"scores[i]: "<<scores[i]<<endl;
    while (!fileIn.eof() && i < MAX_ARRAY)
    {
        sum+=scores[i];
        cout<<endl<<i<<": "<<sum<<endl;
        i++;
        fileIn >> scores[i];
        cout<<"scores[i]: "<<scores[i]<<endl;
			
    }
    numElms = i;

	cout << "\nAvg is: " << average(sum, numElms);
JHus00 commented: Thanks! +1
MyrtleTurtle 52 Junior Poster

Hmm...

try initializing sum to 0, see if that helps.

int numElms, sum=0;

//instead of 
int numElms, sum;
MyrtleTurtle 52 Junior Poster

Maybe your function should return avg instead of sum?

MyrtleTurtle 52 Junior Poster

Because it wouldn't compile. hah. ;)

Um, I guess it's because there are 3 ;'s ? Also, possibly because there's no condition to end the loop.

I wouldn't think it's because there are 2 ;s in a row, because you could do this:

for ( ;; )

//but I wouldn't, because it's an endless loop (unless you have other conditions within the loop to stop it).

MyrtleTurtle 52 Junior Poster

If your class hasn't covered while loops then my solution probably isn't what you want.

But here is some information on while loops anyway. Sorry I could not be more help to you.

MyrtleTurtle 52 Junior Poster

Those are the answers that I would have put. :)

MyrtleTurtle 52 Junior Poster

I would think that your problem is here:

getline(inData,payto);

inData >> payto;
inData >> rate;

You might have better luck with something like:

inData >> payto >> rate; // Read in first values, priming read
  	
while(inData){ //as long as there are numbers to be read (this loop should get all values from the file, in order)
  	
   // whatever you're supposed to do with your two variables will go here
        				
    inData >> payto >> rate; //read next two values
}
inData.close();
MyrtleTurtle 52 Junior Poster

I know we're not supposed to give homework help unless the OP shows an effort, but this does look like you've shown an effort.

These are the ones that don't look right to me:

-- float and double variables should be used:
a. To perform monetary calculations.
b. As counters.
c. To store true/false values.
d. As imprecise representations of decimal numbers.

This is why I think it's not right

-- Which of the following for headers is not valid?
a. for ( int i = 0; i < 10; i++ ).
b. int i = 0;
for ( ; i < 10; i++ ).
c. for ( int i = 0; int j = 5; ; i++ ).
d. All of the above.

(try compiling each of them, you'll see which one it is)

-- Which of the following operations has the highest precedence?
a. Postincrement.
b. Multiplication.
c. Addition.
d. Assignment.

This seems to contradict your answer.

I hope this helps you! Good luck on the test. :)

MyrtleTurtle 52 Junior Poster

I was looking up some things about vectors today. In fact I was actually able to make a program that uses vectors (and actually works correctly!) with the help of two sites: vector and this one from Wiki, so maybe this will be helpful to you as well.

MyrtleTurtle 52 Junior Poster

If you make your 1st textbox invisible, you can assign the value of each button to that textbox (whenever you click a button, just as you have it now) and then when you click on any other textbox make it's value equal to the first one.

It's not the exact thing you want done: Click on a textbox then a button and assign that button's value to the textbox.

It is almost the same though: Click on a button and then a textbox and assign that button's value to the textbox.

Edit: apegram's answer is probably way better than mine, sorry!

@apegram, no offense, and I certainly do not mean to hijack someone's thread, but...

I cannot get your code to work. It gives me errors:

It doesn't like the commas on these lines, but also complains after I change them to ;s as it suggests.

buttons = new List<Button>() { button1, button2, button3, button4, button5 };
            values = new List<string>() { "A", "B", "C", "D", "E" };

            // temp list of boxes
            List<TextBox> boxes = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox5 };

Here's the other error:
'WindowsApplication1.Form1.ActiveTextBox.set' must declare a body because it is not marked abstract or extern

MyrtleTurtle 52 Junior Poster

I have loads of text boxes (coloured blue) and some buttons. Right now if i click a button it only assigns the frame to textbox1. How do i make it assign to the box that my cursor is on (when i click the box)

If I read this correctly, what you want to do is to assign whatever text is in the first box into the second one as well, when you click the second box?

(Using Visual Studio C# Express Edition 2005...yeah I know I need to update) You can do this:

Go to design view. Right click on textBox2. Click Properties (if it isn't already showing). Look to the right, where the properties are displaying. Click on the lightning bolt (events). Scroll down to MouseClick. Double click on it, which should take you to the code & add the MouseClick code. Then just write inside it whatever you want to do when the user clicks the mouse.

private void textBox2_TextChanged_1(object sender, EventArgs e)
      {
   //         textBox2.Text = "Whenever you write something in the box and hit enter, this text will appear.";
          //the next part did not work until I commented this out
//but you had nothing there anyway so it shouldn't matter for your code
    }

        private void textBox2_MouseClick(object sender, MouseEventArgs e)
        {
            textBox2.Text = textBox1.Text; //this will make the text in the 1st box appear in the 2nd one when the user clicks this box
        }
MyrtleTurtle 52 Junior Poster

The first code seems to be better, to me. The second one won't even compile for me, as is. You're trying to use the variable r without passing it into the functions, and I cannot see where you declare startindex or pivot.

Also you still have the same issue as before. a[10] means there are ten spots in the array, right? And it starts with 0. So the last spot will be a[9], not a[10].

It seems to me as if you're making this a little more complicated than it really needs to be. I wouldn't even do the partition part.

Here's another example of an insertion sort. It seems to work just fine without the partition.

MyrtleTurtle 52 Junior Poster

Well, for one thing, you are asking for (and later printing) 16 inputs from the user, not 15. Also, meaningful variable names (and indenting) would be nice. :)

Here's an insertion sort I found interesting, and thought it may be helpful to you.

MyrtleTurtle 52 Junior Poster

Yes you can.

Oops. Sorry. Thanks for the correction. :$

Fbody commented: A point for willingness to learn. +1
MyrtleTurtle 52 Junior Poster

Edit:
...it does seem to work when I compile your code, as is. The first line is off, but that's only the formatting (needs to go a bit to the right).

However, I do see this:

int isleapyear;

else if (month == 2 && isleapyear)

You can't compare an int value in your if statement. You could compare it if it was a bool though.

MyrtleTurtle 52 Junior Poster

Check your spelling...

MyrtleTurtle 52 Junior Poster

I need to know why the catch is int i & not int x?

Because i is the 'name' of the exception thrown, and x is the variable name used to store your input data. You could call your exception e or something else, but it shouldn't be the same as the variable you use to store the user's input.

MyrtleTurtle 52 Junior Poster
void printValues(int numI1, int numI2, float numF1, float numF2) ;

int main () {
 
	printValues(4, 3, 3.3, 56.6) ;
}
//test to see it print correctly
void printValues(int numI1, int numI2, float numF1, float numF2) {
	
	printf("1: %d 2: %d 3: %f 4: %f \n", numI1, numI2, numF1, numF2) ;
}

What's wrong with this? It seems to work fine without that other stuff.
//edit: except that 56.6 prints out as 56.599998, for me

I have never used a union (never heard of it, actually, except in math.) Though after looking it up it seems like a pretty neat idea! The only time I've used an enum was when I wanted the data enumerated.

MyrtleTurtle 52 Junior Poster

You just want to know how to print a table? I did it using three for loops, kinda like this:

cout<<"\n";
            for(int x=0 ; x<=3; x++){//prints rows of 4 incremented rates       
                cout<<"\trate"; //print one row, then increment the rate

            }//end for 

            cout<<endl;

            for(int y = 1; y<=12; y++){//makes 12 rows
                cout<<"\nYear "; //beginning of rows, 1st column

                for(int z=0; z<=3;z++){//print rows, 2nd - 4th columns
                //calculate the factor
                    cout<<"\tfactor";

                }//end for z 
            }//end for y
        cout<<endl;
MyrtleTurtle 52 Junior Poster

One equal sign assigns a value. Two equal signs compare values.
Change

if(c[i]%2=0)

to

if(c[i]%2==0)

and it should get rid of that error.

But you probably shouldn't use goto.
You can use a for loop or a while loop or even a do while loop instead.

MyrtleTurtle 52 Junior Poster

And he can fail the project for using code that was not taught and has no business using. The instructor will know that the code was given to him and he's getting others to write his code for him.

Please keep in mind who is asking and their level of knowledge before showing your prowess as a programmer and blowing their mind with higher level techniques. :icon_wink:

Oh....sorry! I was just trying to help.

I do see your point, but in my classes, I always look for other ways to do things, even if the instructor hasn't taught it yet. I try to make my programs the best they can be, regardless of what level has been taught in class. To me, it's best to learn everything you can instead of just the (very limited) amount that they teach in school.

But I don't want to get anyone in trouble, either. :(

MyrtleTurtle 52 Junior Poster

It seems like it would be easier to use prompts than textboxes, since textboxes are for text and I think it's not as easy to use them for math.

I'd do something like this (which is just an example, and not exactly what you wanted to do):

<body>
<script type = "text/javascript">

	var userNum;
	
	document.write("<h1>This program will double the number entered.</h1>");
	
	userNum = (+prompt("Enter a number: ",""));  //get user input
	
	document.write("<h2>Your number doubled is: " + userNum * 2 + "</h2>");  //display results

</script>
</body>
MyrtleTurtle 52 Junior Poster

Oh yeah...I was going to say that, too. Don't use goto.

And, you can't actually control what the user will input. But your program can handle it if they do not enter what you want them to, if you add the code I suggested. :)

MyrtleTurtle 52 Junior Poster

I am not sure about the first part but I can answer the second part. One way to ensure that the input is good (the user enters a number) is to do this:

void clearError(long int& num){ //pass number in by reference so it can be changed within the function
    cin.clear(); //clear the error state
    //ignore characters left in the buffer up to and including the new line character
    cin.ignore(numeric_limits<streamsize>::max(), '\n');      
    //allow them to reenter number
    cout<<"That is not a number. Try again: ";
    cin>>num;
}

int main ()
{

	cout<< " WELCOME TO THE 2 BASED FACTORIAL CALCULATOR " << endl << endl;

start:;

  long number;
  cout << "Please type a number: ";
  cin >> number;
  
  while(!cin.good()){
     clearError(number);    
  }

I like to put it in a function so it can be reused, but you don't have to do it that way.

MyrtleTurtle 52 Junior Poster

You can also create and open your file this way, which is more like what you're doing:

ofstream myfile;

    string filename = "MyNewFile2.txt"; //or whatever name you want
    myfile.open (filename.c_str(), ios::out);
MyrtleTurtle 52 Junior Poster

You can create a file and write to it like this:

#include<iostream>
#include<fstream> // for file reading & writing
#include<conio.h> //for getch()

using namespace std;
int main(){
fstream myfile; //you can use ofstream for writing to a file and ifstream for reading from a file...& probably should
    myfile.open ("MyNewFile.txt"); //put whatever name you want
    if (!myfile) //um, why do you try to write to a file that has not opened?
    {
     //I would show an error if the file doesn't open
     cout<<"File not found.";
//        myfile.close();  how can you close it if you don't have one?
//        myfile.open (filename.c_str(), ios::out);
//
//        avatar.name = name;
//        avatar.init();
//        myfile << "Name = " << avatar.name << endl;
//        myfile << "hp = " << avatar.hp << endl;
//        myfile << "mana = " << avatar.mana << endl;
//        myfile << "level = " << avatar.level << endl;
//        myfile << "strength = " << avatar.strength << endl;
//        myfile << "intelligence = " << avatar.intelligence << endl;
//        myfile << "wisdom = " << avatar.wisdom << endl;
//        myfile << "dexterity = " << avatar.dexterity << endl;
//        myfile << "charisma = " << avatar.charisma << endl;
//        myfile.close();
    }
    else
    {
       // cout << name << " already exists" << endl;
       myfile<<"write something to the file.";
       cout<<"Writing to file.";
       myfile.close();
    }
getch();
return 0;
}
MyrtleTurtle 52 Junior Poster

I tried running your code in FireFox (using notepad++). I created my own gif and named it na.gif, the same as yours. I saved the gif file in the same place as the html file was saved. I made NO changes to your code at all. My na.gif image does show up.

edit: I also tried running it in IE8, and the image shows up there, too.

So...is your gif image saved in the same directory or folder as your html file?

MyrtleTurtle 52 Junior Poster

You know what? I should have done more checking before replying. I think that a matrix actually IS a 2d array (or is made by using one?)

heh...live & learn.

There may yet be an easier way of doing this. Maybe another member will be able to help you. Sorry I wasn't much help, but I tried! :)

MyrtleTurtle 52 Junior Poster

wino

MyrtleTurtle 52 Junior Poster

Have you thought about using a matrix to store the board instead of a 2D array? Of course, you'd still have to compare adjacent positions in the rows/columns.

Maybe you could do something like:

for(int i =0; i<SIZE-1; i++){
   if(rowPos[i] ==rowPos[i+1]){
      points++;
  }
}

Where SIZE is the amount of positions in the row. Then just put it in a function and call it for each row. And make a similar function for the columns.

I'm not sure if this idea would be better or not, since I haven't actually coded it.

You'd have to add checks for if they have two in a row, then two more in a row (assuming your board is always 5x5). Also, the way you want to add your points is different than my code, but it's just an example.

Good luck. :)

MyrtleTurtle 52 Junior Poster

Your problem is that this for loop never runs. You have the greater than/less than sign mixed up.
change

for(n=1; n>=100; n++)

to

for(n=1; n<=100; n++)
MyrtleTurtle 52 Junior Poster

That code works fine for me (in Dev-C++). No errors, no warnings.

MyrtleTurtle 52 Junior Poster

It appears to me that this will make an infinite loop.

for (j=2*i-2;j<i;j--)

You want it to go as long as j<i, but then decrement j each time? Shouldn't do that.

Also, why are you using the same variable (j) for both inner loops?

Something else I see, which may or may not be a problem. The order of operations causes multiplication to be done before subtraction, so I'd do this:

for (j=i;j<2*(i-1);j++)
MyrtleTurtle 52 Junior Poster

Could you show your code? Maybe the problem is in the for loop itself, but it's hard to know that for sure without seeing the code.

MyrtleTurtle 52 Junior Poster

Hello.
I'm new here, too, but I have read the site rules. You're supposed to show an effort before the site's members will help you.

But the answer to your question is yes, there is code to solve that problem.

I'd get user input, put their numbers into an array and then check each number in the array. Or you could use a vector, I guess.

What code have you written so far?

Fbody commented: rule followers are good :) +1
MyrtleTurtle 52 Junior Poster

It seems to me as if your problem is when you try to call your function within your function. I commented it out and the program no longer freezes, although the count is not correct.

Maybe you can work with this, and get it working the way you want:

ptr=str;
for(i=1;i<=strlen(str);i++){                          
//count_a_fnc();
ptr++; //why increment this?  
if (*ptr == 'a'); // added the apostrophe's
count_a++;
}
printf( "%d", count_a); //just to see what your count is...it's not correct though
system ("PAUSE"); //placed outside of loop
MyrtleTurtle 52 Junior Poster

Hi,

I have created a .gif image in photoshop,then i have linked the location in html coding.But i am unable to see that image in internet explorer.I am able to see a red crossed line.How to make that image to display??

It is hard to tell what is going wrong without seeing your code.

Here is one way to get an image to display, provided that the URL of the image source is available (online, so anyone with an internet connection can access it, or in the same directory as your html document if you only want to be able to view it on your computer):

<img src = "http://yourImageSourceHere" alt ="your text here"/>
MyrtleTurtle 52 Junior Poster

You probably want to write

printf ("\n");

instead of

printf ("/n");

:)

MyrtleTurtle 52 Junior Poster

Have you tried writing the text to a file instead of just to the screen? Then when you need to retrieve it, you'll have everything saved in your file.

If that's not the answer, maybe a different compiler would work better?

Dev-C++ wouldn't scroll for me when I used Vista, but on Windows 7 it does. So maybe a different compiler would allow scroll bars so you can see all your text.

MyrtleTurtle 52 Junior Poster

Hello everyone. This forum looks like a nice place. I'm glad to be here.

I'm currently a Junior in college working toward my bachelor's, which hopefully I'll obtain next Spring. I already have an Associate's degree in Computer Programming.

I still have a lot to learn, so I'm hopeful that this site will provide answers to the many questions I have about programming and IT in general.

Also I hope to be able to help others whenever I can.

Thanks for having this nice site. :)