Friday, July 31, 2009

I need help with a c++ program please?

Ok I need to write a program that encrypts and decrypts entered.


It also has to take into account a "shift" value that changes the encryption/decryption output.


like say the value for k is 1, if you enter a shift value of 7 and ask for the program to encrypt k it should print out 8





Here is the program:





#include%26lt;iostream%26gt;


using namespace std;





int menu(){


cout%26lt;%26lt;"Press 1 to encrypt, 2 to decrypt, 3 to quit)"%26lt;%26lt;endl;


int x;


cin%26gt;%26gt;x;


return x;





}


void encrypt(char){


char letter;





cout%26lt;%26lt;"Enter a message to encrypt (! to quit)"%26lt;%26lt;endl;


cin%26gt;%26gt;letter;


while (letter!='!'){


if (letter=='k') cout%26lt;%26lt;"1 ";


followed by 26 more if statements for other letters





cin%26gt;%26gt;letter;


}


return;


}











void decrypt(){


cout%26lt;%26lt;"Enter a message to decrypt (0 to quit)"%26lt;%26lt;endl;


int code;


cin%26gt;%26gt;code;


while (code!=0){


if (code==1) cout%26lt;%26lt;"k";


Followed by 26 more if statements





(will continue in additional details)

I need help with a c++ program please?
looks like you haven't even used the shift value in your encrypt function. you can't use the shift variable defined in main() because it's out of scope in encrypt()





just change the encrypt function to encrypt(int s) -you don't need a char there- then before printing out add s to 1 like this:


if(letter=='k')


{


cout%26lt;%26lt;s+1


}


now you have both problems solved.


good luck and I hope this helped.





ps: since you have 26 options I suggest you use a case construct instead of so many if statements.
Reply:This scares me!!! I took C++ a fews years back and out teacher made us code everything in notebad. I hated that class, good luck because I don't have clue of what is wrong.
Reply:1) u have not included shift value in ur program.u can check it out.


2) to make functions encrypt(int),just replace the prototype and definition arguement type to int and also store them in int too in the function.

augustifolia

Help on C++ string problem?

Write a program that reads a date as a string in the form


10/19/72 and displays the same date in the form October 19, 1972.





All I have so far is this:





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


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


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





int main()


{





string s1, s2, s3;





cout %26lt;%26lt; "\nEnter a date in the form of mm/dd/yy:\n";


cin %26gt;%26gt; s1 %26gt;%26gt; s2 %26gt;%26gt; s3;





//missing





cout %26lt;%26lt; s1 %26lt;%26lt; ' ' %26lt;%26lt; s2 %26lt;%26lt; ', ' %26lt;%26lt; "19" %26lt;%26lt; s3





return 0;


}





When I enter this onto libra to test, there are errors saying that s1, s2, s3 aren't declared.





For the missing part, should it be something like this?


char s[15];


int i;


int January,Febuary,March,April,May,June;





cout %26lt;%26lt; "\nEnter a date in the form of mm/dd/yy:\n";


cin.getline(s,15);





for(i=0;s[i];i++)


{if (((s[0]*=10)+=s[1])==1)


cout %26lt;%26lt; January %26lt;%26lt; ' ';


else if (((s[0]*=10)+=s[1])==2)


cout %26lt;%26lt; Febuary %26lt;%26lt; ' ';


else if (((s[0]*=10)+=s[1])==3)


cout %26lt;%26lt; March %26lt;%26lt; ' ';


}

Help on C++ string problem?
I'd recommend using scanf. Normally I would never recommend this, since scanf is dangerous to use if you don't know your input is well formed, but here it seems like just the thing.





scanf("%d/%d/%d", %26amp;m, %26amp;d, %26amp;y);





This expects to find input of just the form you want, and decodes each section as an integer and stores it in the correct value.





Now, as for decoding the months, I recommend creating an array with the month names and just looking in it.





static string months[] = {


"January", "February", ...


};





Now the program is pretty easy to write. I converted your embedded \n characters to use the endl manipulator, added "using namespace std", which I think you need if you want to use cout and such, and dropped the .h from your header files to use the newer style header files. If you don't have them, you can add the .h back. Here's my code, which compiles and runs (for me) without warnings or errors. Of course, Yahoo! destroys the formatting. :-(





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;string%26gt;


using namespace std;


static string months[] = {


"January", "February", "March", "April", "May", "June",


"July", "August", "September", "October", "November",


"December",


};


int main() {


int m, d, y;


cout %26lt;%26lt; endl %26lt;%26lt; "Enter a date in the form mm/dd/yy:" %26lt;%26lt; endl;


scanf("%d/%d/%d", %26amp;m, %26amp;d, %26amp;y);


cout %26lt;%26lt; months[m-1] %26lt;%26lt; " " %26lt;%26lt; d %26lt;%26lt; ", " %26lt;%26lt; y %26lt;%26lt; endl;


}





Hope this helps!





EDIT:


I said scanf was dangerous if you didn't know your input was well formed. To see what I mean, try running the program and entering gibberish instead of a date. You might get bad output, errors, or even a segfault!





If I were implementing this in a product, I'd probably just read a line of text and then use a regular expression library to try to match the regular expression ([0-9]+)/([0-9]+)/([0-9]+), and issue an error message if it didn't match. Then I could use atoi to convert each substring into a number, and check the range (0%26lt;month%26lt;13, etc.) and validate the date before I did anything else.





There are date/time libraries on the system I could use to get the date formatted in the correct locale (different countries format dates differently, after all), so I wouldn't need an array of month names at all.


C++ help!!! i am getting these errors and don't know how to fix them - first time working with C++?

ok, the user types item number - the program displays the color of the item. like: 12b45 or abg44. the third character determines the name of the color. which in this instance will be: blue and green. THIS IS MY CODE:


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cctype%26gt;





using namespace std;





int main()


{


//declare variables


string itemNum = "";


int location = 0;





//get input from user


cout %26lt;%26lt; "Enter an item number: " %26lt;%26lt; endl;


cin %26gt;%26gt; itemNum;





//calculate shipping charge


while (itemNum != "x" %26amp;%26amp; itemNum != "X")


{


if (itemNum.length() != 5)


cout %26lt;%26lt; "The item number must contain five digits! " %26lt;%26lt; endl;





else if( 'B' == toupper(itemNum[2])


{


cout %26lt;%26lt; "Your color is Blue" %26lt;%26lt;endl;





else if ('G' == toupper(itemNum[2])


{


cout %26lt;%26lt; "the color is green" %26lt;%26lt; endl;





else


cout %26lt;%26lt; "Invalid item!" %26lt;%26lt;endl;


}


}


}

C++ help!!! i am getting these errors and don't know how to fix them - first time working with C++?
You are missing right parenthesis. you have


else if( 'B' == toupper(itemNum[2])





if you take notice, you have two left parenthesis in that line, but only one right one. you need to have a left and right parenthesis around itemNum[2], like you do.... but you need to have parenthesis around the entire 'if' conditional. you have the one on the left, but your forgot the one on the right. In C++, anytime you have a left bracket or parenthesis, you must have a matching right bracket or parenthesis.


so make the line:


else if( 'B' == toupper(itemNum[2]))





You do this another location as well. Change that line too.





You are putting curly braces at the wrong places.





You have:





if(conditional)


code


else if(conditional


{


code


else if( conditional)


{


code


else


code


}


}


}





It should be:


if(conditional)


{


code


}


else if(conditional)


{


code


}


else if(conditional)


{


code


}


else


{


code


}





you see the difference? you are putting them at the end of all the conditionals. The curly brackets are used to separate blocks of code. They say if this condition is met, carry out all of the code between these brackets. The brackets are also why you are getting an error stating you have an else without a matching if statement. If you look at how the brackets are placed you have blocks of code that have 'else' statements without an 'if' statement before it in that block of code. So change the placement of the curly brackets and those problems should go away.





Take note, if you are only doing one statement after an 'if' or 'else if', then you do not need curly brackets. if you have more than one statement that needs to be executed if a condition is met, then you need curly brackets. I often put them in even if I only have one statement after an 'if' conditional because it keeps the code clean and it is easy to add more lines in the future if I discover i need to do so.
Reply:#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cctype%26gt;





using namespace std;





int main()


{


//declare variables


string itemNum = "";


int location = 0;





//get input from user


cout %26lt;%26lt; "Enter an item number: " %26lt;%26lt; endl;


cin %26gt;%26gt; itemNum;





//calculate shipping charge


while (itemNum != "x" %26amp;%26amp; itemNum != "X")


{


if (itemNum.length() != 5)


{


cout %26lt;%26lt; "The item number must contain five digits! " %26lt;%26lt; endl;


}


else if( 'B' == toupper(itemNum[2])) %26lt;%26lt;- Added ')'


{


cout %26lt;%26lt; "Your color is Blue" %26lt;%26lt;endl;


} %26lt;%26lt;- Added '}'


else if ('G' == toupper(itemNum[2])) %26lt;%26lt;- Added ')'


{


cout %26lt;%26lt; "the color is green" %26lt;%26lt; endl;


} %26lt;%26lt;- Added '}'


else


cout %26lt;%26lt; "Invalid item!" %26lt;%26lt;endl;


cout %26lt;%26lt; "Enter an item number: " %26lt;%26lt; endl;


cin %26gt;%26gt; itemNum;


} // Removed the extra }} and moved input within loop


return 0;


} //end of main function





This code should work now.
Reply:Both lines that begin with "else if" need a ) at the end. This is, by the way, explicitly stated in the error message you provided.





You are also missing closing braces after each of your else blocks.
Reply:find another profession, programming doesn't seem to be your strong skill..


Help with C++ please!! i am getting this error and don't know how to fix it:?

Error1error C2181: illegal else without matching if. Line 41


this program should display shipping charge based on the ZIP code entered by the user. this is my code:


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::string;


using std::setprecision;


using std::fixed;








int main()


{


//declare variables


string zipCode = "";


string beginZipCode = "";





//get input from user


cout %26lt;%26lt; "Enter a zip code: " %26lt;%26lt; endl;


cin %26gt;%26gt; zipCode;





//calculate and display taxes and net pay


while (zipCode != "x" %26amp;%26amp; zipCode != "X")


{


if (zipCode.length() != 5)


cout %26lt;%26lt; "The zip code must contain five digits! " %26lt;%26lt; endl;


else


{


beginZipCode = zipCode.substr(0, 3);





if (beginZipCode == "605")


cout %26lt;%26lt; "Shipping charge: $25.00" %26lt;%26lt;endl;


else if (beginZipCode == "606")


{


cout %26lt;%26lt; "Shipping charge: $30" %26lt;%26lt;endl;


else


cout %26lt;%26lt; "Invalid code!" %26lt;%26lt;endl;

Help with C++ please!! i am getting this error and don't know how to fix it:?
You're missing some brackets. Try this:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::string;


using std::setprecision;


using std::fixed;








int main()


{


//declare variables


string zipCode = "";


string beginZipCode = "";





//get input from user


cout %26lt;%26lt; "Enter a zip code: " %26lt;%26lt; endl;


cin %26gt;%26gt; zipCode;





//calculate and display taxes and net pay


while (zipCode != "x" %26amp;%26amp; zipCode != "X")


{


if (zipCode.length() != 5)


cout %26lt;%26lt; "The zip code must contain five digits! " %26lt;%26lt; endl;


else


{


beginZipCode = zipCode.substr(0, 3);





if (beginZipCode == "605")


cout %26lt;%26lt; "Shipping charge: $25.00" %26lt;%26lt;endl;


else if (beginZipCode == "606")


cout %26lt;%26lt; "Shipping charge: $30" %26lt;%26lt;endl;


else


cout %26lt;%26lt; "Invalid code!" %26lt;%26lt;endl;


}


cout %26lt;%26lt; "Enter a zip code: "%26lt;%26lt;endl;


cin %26gt;%26gt;zipCode;





}


return 0;


} //end of main function
Reply:Only one } ???


A little help with C++?

cin %26gt;%26gt; menu;


if ( menu == 1) {


while (y != 0) {


system("cls");


cout %26lt;%26lt; "Welcome to the CALCULATOR OF DEATH" %26lt;%26lt; endl %26lt;%26lt; "Enter 2 numbers to add" %26lt;%26lt; endl;


cin %26gt;%26gt; haha;


cin %26gt;%26gt; haha2;


cout %26lt;%26lt; haha + haha2 %26lt;%26lt; endl;


system("PAUSE");


cout %26lt;%26lt; "Again? (Enter 0 to Exit)" %26lt;%26lt; endl;


cin %26gt;%26gt; y;


system("cls");


}


}





if (menu == 2) {


cout %26lt;%26lt; "Editor???" %26lt;%26lt; endl %26lt;%26lt; "Virus Found Shutting Down!!" %26lt;%26lt; endl;


system("pause");


for (double z = 0; z %26lt; 999; z++) {


cout %26lt;%26lt; "OWNED" %26lt;%26lt; endl;


}


}


}


system("PAUSE"); and the rest i left out the start and finish because i only get 100 chars but i want it to say shutting down in 10


9


8


7


6


5


4


3


2


1


do you get what i am saying

A little help with C++?
change the loop to:


for (int z=999;z%26gt;=0;z--) {


cout %26lt;%26lt; z %26lt;%26lt;endl;


}


that should print the numbers out in reverse order (i hope!)
Reply:cout %26lt;%26lt; "Virus Alert: Shutting down" %26lt;%26lt; endl;





for(int down=9; down%26gt;0; down++)


{





for(double z=0;z%26lt;999;z++){}





cout %26lt;%26lt; down %26lt;%26lt; endl;


}


cout %26lt;%26lt; "\n\n Critical Windows Files Deleted!! Shut Down Failed..";

nobile

Can someone who knows C++ help me?

I can't get my code to run. Could someone look at it and tell me whats wrong.





//


// Program: Sum of Squares


// Written by: E.A.


// Date: Jan 13, 2008


//


#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;





using namespace std;





int main()


{


int m, n, Side_one, Side_two, Hypotenuse; //variables used in program





//input data


//calculate side one





cout %26lt;%26lt; "Enter m:\n ";


cout %26lt;%26lt; "Side one: " %26lt;%26lt; pow(float (m),2) - pow(float (n) ,20);


cin %26gt;%26gt; Side_one;





//calculate side two


cout %26lt;%26lt; "Enter n:\n ";


cin %26gt;%26gt; "Side_two: " %26lt;%26lt; 2*m*n;


cin %26gt;%26gt; Side_two;


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





//calculate hypotenuse


cout %26lt;%26lt; "Hypotenuse: " %26lt;%26lt; sqrt(pow(float (Side_one), 2)+pow(float (Side_two),2));


cin %26gt;%26gt; Hypotenuse;


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





return (0);


}


//


//End of main


//

Can someone who knows C++ help me?
Your problem is in line 24, where you have the following statement...





cin %26gt;%26gt; "Side_two: " %26lt;%26lt; 2*m*n;





The cin statement can't take a value into a string "Side_two:". Make the cin statement point to a variable and all will be fine.
Reply:The person above is correct. I'm rusty on C++ but there is something wrong having a %26lt;%26lt; symbol in the cin statement:





cin %26gt;%26gt; "Side_two: " %26lt;%26lt; 2*m*n;





There should be a cout when using %26lt;%26lt;.


How to clear all the text when loop in C++?

yeh thank you too all i've figure out looping but in annoyed of the text when im ask to try again. is there any way to clear the past data and like reset the whole thing when in loop?


here's my source code pls help agian.








#include %26lt;iostream%26gt;


using namespace std;


int main ()


{


char answer;


int temperature, pressure;





cout%26lt;%26lt; "Would you like to test the temp? y/n \n";


cin%26gt;%26gt;answer;





while (answer=='y' || answer=='Y')


{


cout%26lt;%26lt; "Input temperature: \n";


cin%26gt;%26gt;temperature;


cout%26lt;%26lt; "Input pressure: \n";


cin%26gt;%26gt;pressure;





if (temperature%26gt;100 || pressure%26gt;200)


{ cout%26lt;%26lt;"Warning End of The World. \n" ;


}





else


{


cout%26lt;%26lt;"It's Fine. \n" ;


}


cout%26lt;%26lt; "Would you like to test the temp again? y/n \n";





cin%26gt;%26gt;answer;





}//ends while loop





return 0;





}

How to clear all the text when loop in C++?
Yes, and this should do nicely.





/** simple screen clearing function **/


void myClearScreen(int lines)


{


while (lines--)


cout %26lt;%26lt; endl;


}








//use it like so


int main()


{





// To clear the "usual" 25 line screen:


myClearScreen(25);





//Very flexible ... maybe you just want to push a few lines up:


myClearScreen(7);





return 0;


}





/////// I would put it here in your code:


cout%26lt;%26lt; "Would you like to test the temp again? y/n \n";


cin%26gt;%26gt;answer;


if(answer == 'y' || answer == 'Y' )


myClearScreen(50);
Reply:Unfortunately, there is on standard way to clear a terminal screen in C/C++. You'll have to use OS specific and library dependant methods. Here's a site that gives you some instructions on how to do it in Linux and Windows.





http://www.irule.be/bvh/c++/howto_portab...