Monday, May 24, 2010

Ok last time im going to try this. Im coming up with a undeclared identifier error in main. this is c++?

/***************************************...


*


* Program name: Lab 10


* Author : Daniel J. Carr


* Date : December 10th, 2007


* Course/selection: CSC-110-003


* Program Description:


*


*


*


**************************************...


/************************** Compiler Directives **********************/


// libraries


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;





using namespace std;


/*********************** Global Data Declarations ********************/


/*************************** Function Prototype **********************/





void chop(float chipmunk,int%26amp; head,float%26amp; tail);





/*************************************...


*


* Function name: Main


* Author: Daniel J. Carr


* Date: December 10th, 2007


* Function Description:


*


* Pseudocode:


* Level 0


* -------


* Enter a float


* Chop it


* Output pieces


*


*


* Level 1


* -------


* Enter a float


* Display "Enter a floating point number----%26gt; "


* Input Chipmunk


* Chop it


* Call funtion chop to split chipmunk into head and tail


* Output Pieces


* Display "The whole number---%26gt; "%26amp; head %26amp; EOL


* Display "The Deciman number-%26gt; "%26amp; tail %26amp; EOL


*


**************************************...


int main()


{


//Varables


float chipmunk;


//arguements











cout %26lt;%26lt; "Enter a floating point number----%26gt; " %26lt;%26lt; endl;


cin %26gt;%26gt; chipmunk;





//Call function chop





cout %26lt;%26lt; "The whole number----%26gt; "%26lt;%26lt; head %26lt;%26lt; endl;


cout %26lt;%26lt; "The Decimal number--%26gt; "%26lt;%26lt; tail %26lt;%26lt; endl;








//Indicate successful termination of program





return 0;


}





//End main


/*************************************...


*


* Function name: Chop


* Author: Daniel J. Carr


* Date: December 4th


* Function Description:


*


* Pseudocode:


* Level 0


* -------


* Chop it


*


* Level 1


* -------


* Chop it


* whole = int(number)


* Decimal =|number-whole|


*


**************************************...





void chop(float chipmunk,int%26amp; head,float%26amp; tail)


{


// Variables





// Arguements


chipmunk = head;


tail = abs(chipmunk-head);








}

Ok last time im going to try this. Im coming up with a undeclared identifier error in main. this is c++?
//Call function chop





cout %26lt;%26lt; "The whole number----%26gt; "%26lt;%26lt; head %26lt;%26lt; endl;


cout %26lt;%26lt; "The Decimal number--%26gt; "%26lt;%26lt; tail %26lt;%26lt; endl;








Two problems


1) Main never calls the function chop (logic problem)


call chop(chipmunk,headmain,tailmain)


from the main program and declare variables headmain and tailmain in the main function. Also set headmain = 1 and tailmain = 1.f to avoid the variable is used without being set error.





2) In


cout %26lt;%26lt; "The whole number----%26gt; "%26lt;%26lt; head %26lt;%26lt; endl;


cout %26lt;%26lt; "The Decimal number--%26gt; "%26lt;%26lt; tail %26lt;%26lt; endl;


you try to use the variables declared ONLY inside the chop function in the main program (ONLY within the chop function can the head and tail variables be used)





Just move these statements inside the chop function (IE at the end of it) and they should work :-)
Reply:"head" and "tail" are never declared in main(). You attempt to set them, but they don't have a type associated with them or anything.





Also in chop() you are setting chipmunk, not head.
Reply:You are trying to reference 2 varibles that have not been declared.





Make it look like this...





//Varables


float chipmunk;


int head;


float tail;





Also, since 'abs' returns a double you should use the 'fabs' function instead.





2nd post:





Thats because you declared them, but not yet used them.


How do i keep the function getline for assigning to the next variable in C++?

when i run the program if u put a space in any of the strings it assigns the stuff after the space to the next string. how do i stop it so that the entire phrase is one and not two?





Code:


#include %26lt;iostream.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string%26gt;


using namespace std;


string adja;


string namea;


string placea;


string adjb;


string thinga;


string nouna;





int main()


{


cout%26lt;%26lt;"Name a word that fits the description giving."%26lt;%26lt;endl;


cout%26lt;%26lt;"an adjetive"%26lt;%26lt;endl;


getline(cin,adja);


cout%26lt;%26lt;"a name"%26lt;%26lt;endl;


getline(cin,namea);


cout%26lt;%26lt;"a place"%26lt;%26lt;endl;


getline(cin,placea);


cout%26lt;%26lt;"a adjetive"%26lt;%26lt;endl;


getline(cin,adjb);


cout%26lt;%26lt;"a thing/noun"%26lt;%26lt;endl;


getline(cin,thinga);


cout%26lt;%26lt;"a thing/noun"%26lt;%26lt;endl;


getline(cin,nouna);


cout%26lt;%26lt;adja%26lt;%26lt;" "%26lt;%26lt;namea%26lt;%26lt;" was walking home from "%26lt;%26lt;placea%26lt;%26lt;" when a "%26lt;%26lt;adjb%26lt;%26lt;endl;


cout%26lt;%26lt;thinga%26lt;%26lt;" threw a "%26lt;%26lt;nouna%26lt;%26lt;" on top of "%26lt;%26lt;adja%26lt;%26lt;" "%26lt;%26lt;namea%26lt;%26lt;endl;


system("PAUSE");


return 0;


}

How do i keep the function getline for assigning to the next variable in C++?
It seems to work fine for me, and thats exactly what getline is for. What compiler are you using?
Reply:I hardly do anything in c++ anymore (C and C# mostly), but there are different ways to retrieve user input, some will split at the space, others will read until the end of the line. In C for example you have getc, read, etc.





I'd look at it from that angle.

flower garden

Complete the function .- Prints out the decimal digits of number in reverse order using c++?

#include %26lt;iostream%26gt;





using namespace std;





const int NUMBER_BASE = 10;





void reverseDigits(int number)


{





/* You are to write the code here to complete this function */





}





int main()


{


int n;


cout%26lt;%26lt;"Enter a 7 Digit number : ";


cin%26gt;%26gt;n;


reverseDigits(n);


return 0;


system("pause");


}

Complete the function .- Prints out the decimal digits of number in reverse order using c++?
use the shift operator %26gt;%26gt; for this.


there probably is a one line but this will also work a binary reverse. not exactly what you want ...





int x = 0;


for (int i = 0;i%26lt;32) { // 32 size of int


x = x%26lt;%26lt;1;


x = x + n%2;


n%26gt;%26gt;1;


}





//


int x = 0;


for (int i =0;i%26lt;7;i++) {


x = x + n%10;


n = n/10;


x *= 10;


}








and ? does it work already ?
Reply:It would be easier to store each digit in a different variable. Then use cout to print them in reverse order.





cout %26lt;%26lt; 7 %26lt;%26lt; 6 %26lt;%26lt; 5 %26lt;%26lt; 4 %26lt;%26lt; 3 %26lt;%26lt; 2 %26lt;%26lt; 1;


Please I want to connect two pointers and it is not working. i am programming in c++ with dev compiler.help me

/*i want to connect frisk pointer and blackee pointer please help me.thanks*/





#include %26lt;iostream%26gt;


using namespace std;


class Cat


{


public:


int GetAge();


void SetAge (int age);


void Meow();


Cat *blackee;


private:


int itsAge;


};


int Cat::GetAge()


{


return itsAge;


}


void Cat::SetAge(int age)


{


itsAge = age;


}


void Cat::Meow()


{


cout %26lt;%26lt; "Meow.\n";


}


int main()


{


Cat *Frisky;


Cat *blackee;


blackee-%26gt;SetAge(4);


Frisky-%26gt;blackee;





cout %26lt;%26lt; "Frisky is a cat who is " ;


cout %26lt;%26lt; Frisky/*.GetAge()*/ %26lt;%26lt; " years old.\n";


return 0;


}

Please I want to connect two pointers and it is not working. i am programming in c++ with dev compiler.help me
OK First of all you need to understand the difference between a pointer and an instance of a class.





Please read the references and they should explain to you why


blackee-%26gt;SetAge(4) is causing your program to crash and how to fix it.





Namely you need to create instances of Cat not just pointers to them. As for connecting them the pointers tutorial should help you out with that.
Reply:Its been a while since I have written in C, but I think there are 3 things I see:








(1) You need to allocate the memory for blackee based on your code here.





ie: Cat *blackee = new Cat();








(2) The second is on the assignment of Frisky to blackee, it should just be:





Friskey = blackee;








(3) Although it is commented out, you appear to be using the '.' operator for accessing the GetAge() method on Frisky. This should be the -%26gt; operator (because you are accessing the method on the pointer to the object). If you were accessing it on the object (ie: Cat Friskey;), then you could use the '.'.








In this instance, the code would have 2 pointers to the same Cat object. I am not sure if you need both objects (may if the assignment asks for it perhaps), but you could get away with the followibng code instead:





int main()


{


Cat *Friskey = new Cat();


Friskey -%26gt; SetAge(4);





cout %26lt;%26lt; "Friskey is " %26lt;%26lt; Friskey-%26gt;GetAge() %26lt;%26lt; " years old.\n";





return 0;


}








Although it might be interesting for you to do the following:





int main()


{


Cat *Friskey = new Cat();


Cat *blackee;





blackee = Friskey;





Friskey -%26gt; SetAge(4);





// both should be the same age.


cout %26lt;%26lt; "Friskey is " %26lt;%26lt; Friskey-%26gt;GetAge() %26lt;%26lt; " years old.\n";


cout %26lt;%26lt; "Friskey is " %26lt;%26lt; blackee-%26gt;GetAge() %26lt;%26lt; " years old (via blackee pointer).\n";





return 0;


}


How do you take the sin of a function in c++?

I am trying to perform sin(pi*0.5), but I cant get it to work. Here is my code:


#include%26lt;iostream%26gt;


#include%26lt;math.h%26gt;


using namespace std;


const double PI = 4.0*atan(1.0)


int main()


{


x = sin( pi*0.5 );


cout %26lt;%26lt; x;


}


It will not give the correct answer. Any help?

How do you take the sin of a function in c++?
error 1 -- x should be declared like





double x;





error 2 -- there is a ; at the end of const double ...





error 3 -- the const is PI all caps.





It works on my machine.


I would like to retrieve the output for exp(-1/2x) with a visual c++ compiler but all i get is 1. Please help.

#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;





int main()


{


using namespace std;


float y,x;





for(x=0;x%26lt;8;)


{


y = exp((-1/2)*x);


cout%26lt;%26lt;"y= "%26lt;%26lt;y%26lt;%26lt;"\n";


x = x + 0.1;


}





return 0;


}





Here's my attempt to code the function. What should I add?

I would like to retrieve the output for exp(-1/2x) with a visual c++ compiler but all i get is 1. Please help.
The problem lies in your "(-1/2)" portion of your code...change that to be "-0.5" ... that will solve your problem..





The reason why it didnt work is because in C/C++ when you have take 1/2 that says to the CPU to take two integers and the result is an integer and since 1/2 is 0.5 as a float BUT as an integer it is "0" so now your line will always try to do a exp(0) which means y will always equal to "1"....So either you change (-1/2) to be (-0.5) or you add a ".0" to the 1/2 ... such as "-1.0/2" or "-1/2.0" or "-1.0/2.0"...All those can fix your problem but the most elegant and fastest for the process would be to use "-0.5".

edible flowers

I can't get this program to work. What am i doing wrong .C++?

Write a funtion that uses a Rectange structure reference variable as its parameter and stores the user's input


in the structure's members.








#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;string%26gt;


using namespace std;





struct Rectangle


{


int lenght;


int width;





};





void gettangle(Rectangle%26amp;);








int main()


{


Rectangle len;


gettangle (len);


return 0;


}





void gettangle(Rectangle %26amp;value)


{


cout %26lt;%26lt; "Enter Lenght:";


cin %26gt;%26gt; value.lenght;


cout %26lt;%26lt; "Enter Width:";


cin %26gt;%26gt; value.width;





}


void gettangle(Rectangle value)


{


cout %26lt;%26lt; value.lenght %26lt;%26lt; ":" %26lt;%26lt;value.width%26lt;%26lt; ":"%26lt;%26lt;endl;


}

I can't get this program to work. What am i doing wrong .C++?
Man, you're code is pretty hashed up. I don't know what you're trying to do in some of it.


First off, I see two Functions both with void type that operate on a given Rectangle's members (length, width). Both of which are named gettangle().


One of these functions takes the variable name, and the other takes the variable address. You only have one prototype above main. You have


void gettangle(Rectangle%26amp;);


you will also need one that takes a rectangle object (leave off the ampersand)


Second to that, your first function prototype is wrong and if you build your second prototype that you need the same way, it will be wrong too. You need to add in the variable name. You should have two prototypes and they will look like this.


void gettangle(Rectangle %26amp;value);


and


void gettangle(Rectangle value);


Now you're prototypes are right, let's move down to your functions. The first function you have is taking an address. Inside the function, to access the members of the rectangle object, you can't use the dot operator. Right now you're code looks like this "cin %26gt;%26gt; value.lenght;" It needs to look like this "cin %26gt;%26gt; value-%26gt;lenght;" Do you see the difference. You'll need a hyphen and the right arrow character to access the object members length and width.


That should do it for your functions.


Your main however is only going to run the program and it is going to run your second function running this code."{


cout %26lt;%26lt; value.lenght %26lt;%26lt; ":" %26lt;%26lt;value.width%26lt;%26lt; ":"%26lt;%26lt;endl;


}"


You're going to get output, but your rectangle variables haven't been set. You need to add the code "gettangle(%26amp;len);" in your main for it to ask you for the inputs before it outputs them. I think that's it. Make those changes and post if you're still not running.


BTW, the person above addressed that the compiler couldn't tell which function to use. That is correct, however if you have two prototypes up there, one that looks like this


gettangle(Rectangle%26amp; value);


(A note on the line of code above, I am not sitting at a compiler right now, so I can't check to see, but I'm not sure that that is where the ampersand goes to let the compiler know that it will be using an address. It may go just before the name like this %26amp;Rectangle, or it could go somewhere else. I just don't remember. So that may mess you up. Looking it up should be easy if you have a book or soemthign though.)


and another that looks like this


gettangle(Rectangle value);


it will be able to tell the difference just by whether you're passing in a object or an address. There isn't a need to rename the function however I would just because it's a good habit not to use the same function name for multiple operations, especially operations that have nothing to do with each other.
Reply:Generally if you provide the error message, it helps.





I think the problem here is that you have these two functions:





void gettangle(Rectangle %26amp;value);


void gettangle(Rectangle value);





Because both take a Rectangle, the C++ compiler can't tell which to use. You should rename the 2nd one to puttangle or similar (or change the function signature).
Reply:why r u having two gettangle function??


the program is already correct. if the problem is u not getting the output(cout%26lt;%26lt;value...)because u got 2 gettangle fuction.





ps:use system("pause"); in main fuction to see output :P