Sunday, August 2, 2009

One more c++ question (hopefully last)?

ok so my program seems to work now, the only thing that doesnt quite work is the output that prompts the user to input an encryption or decription


heres how my program goes


int option;


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


cin%26gt;%26gt;option;


while (option!=3){


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


char letter;


cin%26gt;%26gt;letter;


while (letter!='!'){


a bunch of if statements


}


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


rest of program isnt relevant


when i run the program if i choose to input a 2 in the initial prompt it will not output the "Enter a message to decrypt (0 to quit)"


what do i need to do so that it will?

One more c++ question (hopefully last)?
I'd try using an else if statement, but here's the real problem, you need to encase everything after the if (option == 1) statement to the if(option ==2) in brackets, or it will start off from the cin%26gt;%26gt;letter statement even if you choose 2.
Reply:I first write your program in indented mode then tell you what is wrong with it. you too try to use indentation in writing your codes.


and I also write line numbers to which i refer in my explanation.


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


1:int option;


2:


3:cout%26lt;%26lt; "Enter 1 to encrypt, 2 to decrypt, 3 to quit"%26lt;%26lt; endl;


4:cin%26gt;%26gt;option;


5:


6:while (option!=3)


7:{


8: if (option==1)


9: cout%26lt;%26lt;"Enter a message to encrypt (! toquit)"


10: %26lt;%26lt;endl;


11:char letter;


12:cin%26gt;%26gt;letter;


13:while (letter!='!')


14:{


15: a bunch of if statements


16:}


17:if (option==2)


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


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


WHY YOUR PROGRAM GOES WRONG !


first your program read in the option variable and if it is not 3 enters the while loop.


consider you enter 2. Now the value of option equals 2.


in the while loop the first if statement is not executed because option == 1 is false.


then your program executes lines 11, 12, 13, 14, 15, 16.


You must put theses lines plus lines 9 and 10 in a { } block so these lines execute only if option == 1.


You can ask any other questions if you want by sending an e-mail to me at os56k@yahoo.com or send an IM if I'm on-line in Yahoo Messenger.


Question about C++. Trying to write a program that configures miles driven and miles per gallon.?

Input information is gallons used, starting mileage, and ending mileage. I need to configure the miles driven and the miles per gallon.





My problem is coming in because I need to be able to do this in a loop. Here is the code I have so far:





do


{


cout %26lt;%26lt; "Enter the gallons used (Enter -1 to End): ";


cin %26gt;%26gt; gallonsUsed;


totalGallonsUsed = (totalGallonsUsed + gallonsUsed);





cout %26lt;%26lt; "Enter the ending mileage: ";


cin %26gt;%26gt; endingMileage;





milesDriven = (endingMileage - milesDriven);


cout %26lt;%26lt; "Miles driven: " %26lt;%26lt; milesDriven %26lt;%26lt; endl;





totalMilesDriven = totalMilesDriven + milesDriven;





milesPerGallon = (milesDriven / gallonsUsed);


cout %26lt;%26lt; "The miles/gallon for this tank was: " %26lt;%26lt; milesPerGallon;





overallMilesPerGallon = (totalMilesDriven / totalGallonsUsed);

Question about C++. Trying to write a program that configures miles driven and miles per gallon.?
Have a do ... while loop like this





cout %26lt;%26lt; "Enter the gallons used (Enter -1 to End): ";


cin %26gt;%26gt; gallonsUsed;





do


{


///////// Calculation Code Goes here


totalGallonsUsed = (totalGallonsUsed + gallonsUsed);





cout %26lt;%26lt; "Enter the ending mileage: ";


cin %26gt;%26gt; endingMileage;








////////////////////////////////


/// ask again for input


//////////////////////////////


cout %26lt;%26lt; "Enter the gallons used (Enter -1 to End): ";


cin %26gt;%26gt; gallonsUsed;


totalGallonsUsed = (totalGallonsUsed + gallonsUsed);


} while(gallonsUsed%26gt;0)
Reply:Check these points:


%26gt;%26gt; Initialized totalGallonsUsed, totalMilesDriven.


%26gt;%26gt; Condition given for while should be while(gallonsUsed!=-1).





This should solve your problem.

liama-song

Basic C++ Programming Help?

I am trying to figure out 3 simple things that I really can't figure out...





1) total odd numbers for my program when I input "3" scores of 53,54,67.


2) determine the highest number between those scores which is "67" .


3) How do I end the program by pressing "x" at the end, but it starts over when I press any other letter





#include %26lt;iostream%26gt;





using namespace std;





int main()


{





int counter = 1;


int totalodd = 0;


int num1;


int scores;


char letter;


int numbers;





cout%26lt;%26lt;"How many scores are you entering?"%26lt;%26lt;endl;


cin%26gt;%26gt;num1;





for (int counter=1; counter%26lt;=num1; counter++)


cout%26lt;%26lt;"Enter an integer: "%26lt;%26lt;endl;


for (int counter=1; counter%26lt;=num1; counter++)


cin%26gt;%26gt;scores;





if ( ( scores % 2) != 0 )


totalodd = totalodd + scores;





cout%26lt;%26lt;"You entered "%26lt;%26lt;totalodd%26lt;%26lt;" odd numbers."%26lt;%26lt;endl;


totalodd++;





cout%26lt;%26lt;"Finish...press 'x' to exit program. any other key to run again."%26lt;%26lt;endl;

Basic C++ Programming Help?
The obvious bug I see is that your for() loops only apply to the next statement. If I enter 3 at the first prompt, you will execute the cout 3 times (showing the prompt over and over) then ask for a number 3 times, overwriting the first 2, then do the calculation on the last number only.





You need to group the prompt (cout0, cin, and calculation all within each pass of the loop by using { and } correctly.





Try:


for (int counter=1; counter%26lt;=num1; counter++)


{


cout%26lt;%26lt;"Enter an integer: "%26lt;%26lt;endl;





cin%26gt;%26gt;scores;





if ( ( scores % 2) != 0 )


totalodd = totalodd + scores;


}
Reply:A second issue is that you need to store the highest value somewhere, maybe in a variable named "highest". After the first value gets read into "scores", assign that as the highest value, for later values you must compare to the previous highest:





if (counter == 1) // 1st value is highest by default


highest = scores;


else if (scores %26gt; highest) // later values get compared to previous highest


highest = scores; // if new value%26gt;previous highest, then make that the highest


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.


Can you please check my c++ program?

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


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


#include %26lt;apstring.cpp%26gt;


void main();


{


do


{


apvector%26lt;int%26gt;age(5);


int num=0, a, b;


apstring choice;


cout%26lt;%26lt;"Enter the total sale from Monday through Friday: ";


for(int num1=0; num1%26lt;5; num1++;)


{


cin%26gt;%26gt;age[num];


num++;


}


cout%26lt;%26lt;"Select a number for the day of the week: \n";


cout%26lt;%26lt;"Monday = 1 \n";


cout%26lt;%26lt;"Tuesday = 2 \n";


cout%26lt;%26lt;"Wednesday = 3 \n";


cout%26lt;%26lt;"Thursday = 4 \n";


cout%26lt;%26lt;"Friday = 5 \n";


cin%26gt;%26gt;a;


b=a-1;


cout%26lt;%26lt;age[b]%26lt;%26lt;"\n";


cout%26lt;%26lt;"Do you wish to continue \n";


cin%26gt;%26gt;choice;


}while(choice!="No")


}





It is supposed to ask for the information, then the user picks what day's information they want to view, then the program should ask if they want to continue, if cout%26lt;%26lt;"no"; program should stop.

Can you please check my c++ program?
so does it work? there is no way for us to check. What does it do? what doesn't it do? You should also post what your header (.h) and the other .cpp file does, because without it, there is no way for us to tell what it is doing, or what it isnt doing. For example, can you declare *? I dont think you can, but if you say you can in the header or other cpp file, then I guess you can. But I dont know.





(also, i would repost this, as this question has reached below the bottom of the list =P)
Reply:I did not find any problem.


Please help me with my C++ program! please!!?

this program should display the number of days in a month, but it's not working why? this is my code:


#include %26lt;iostream%26gt;


using std::cout;


using std::cin;


using std::endl;





int main ( )


{


//declare varibles


int days[12] = {31, 28, 31, 30, 31, 30, 31, 30, 30, 31, 30, 31};


int monthNumber =0;





//get input from user


cout %26lt;%26lt; "Enter the month's number: " %26lt;%26lt; endl;


cin %26gt;%26gt; monthNumber;





while (monthNumber =! -1)


if (monthNumber %26gt;= 1 %26amp;%26amp; monthNumber %26lt;= 12)


cout %26lt;%26lt; "Number of Days: "%26lt;%26lt; days [monthNumber - 1];


else


cout %26lt;%26lt; "Invalid Input" %26lt;%26lt; endl;


//end if


cout %26lt;%26lt; "Enter the month's number: " %26lt;%26lt; endl;


cin %26gt;%26gt; monthNumber;


return 0;


} //end while

Please help me with my C++ program! please!!?
while (monthNumber =! -1)





That should be:


while(monthNumber != -1)





You are also not putting the while stuff into a curly bracket. So It doesn't repeat the whole block of code.





while(monthNumber != -1)


{





if (monthNumber %26gt;= 1 %26amp;%26amp; monthNumber %26lt;= 12)


cout %26lt;%26lt; "Number of Days: "%26lt;%26lt; days [monthNumber - 1] %26lt;%26lt; endl;


else


cout %26lt;%26lt; "Invalid Input" %26lt;%26lt; endl;


//end if


cout %26lt;%26lt; "Enter the month's number: " %26lt;%26lt; endl;


cin %26gt;%26gt; monthNumber;


}
Reply:try taking the return 0; from outside the while. that is making you exit the program after you insert a number for the second time without seeing the answer.





And can you tell us what is not working? what happens?

garden state

Whats the mistake in this c++ program.?

#include%26lt;iostream%26gt;





int main()





{


std::cout %26lt;%26lt; " X = " %26lt;%26lt; '\n';





int x;


std::cin%26gt;%26gt;x;


std::{cout %26lt;%26lt; "n= " %26lt;%26lt; '\n';





int n;


std::cin%26gt;%26gt; n;





int ans = x^n;


std::cout %26lt;%26lt; "Answer: " %26lt;%26lt; ans %26lt;%26lt; '\n';





return 0;


}


--------------------------------------...


this is giving error ---------------





Parse Error, expecting `'}''


':cout %26lt;%26lt; " X = "%26lt;%26lt; '\n''


aborting compile

Whats the mistake in this c++ program.?
std::{cout %26lt;%26lt; "n= " %26lt;%26lt; '\n';


remove { bracket from there





std::cout %26lt;%26lt; "n= " %26lt;%26lt; '\n';


it will not give you parse error since number of opening bracket must be equal to closing bracket
Reply:1)you left the ".h" in the header file


2)std::{cout %26lt;%26lt; "n= " %26lt;%26lt; '\n'; // unwanted open braket


std::cout %26lt;%26lt; "n= " %26lt;%26lt; '\n'; //change to this one





best if you declare the int x and n int begining to avoid mistake





Hope it helps you.................♪♪♪♪♪♪♪♪
Reply:I use turbo C compiler. It does not accept std::cout.


So i have made some changes to make it run properly.





The error was


You have used { unnecessarily in cout.





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





int main()





{


cout %26lt;%26lt; " X = " %26lt;%26lt; '\n';





int x;


cin%26gt;%26gt;x;


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





int n;


cin%26gt;%26gt; n;





int ans = x^n;


cout %26lt;%26lt; "Answer: " %26lt;%26lt; ans %26lt;%26lt; '\n';





return 0;


}
Reply:std::{cout %26lt;%26lt; "n= " %26lt;%26lt; '\n'; //error is there i think..the {


std::cout %26lt;%26lt; "n= " %26lt;%26lt; '\n'; //change to this one





good luck
Reply:write cout%26lt;%26lt;"X="%26lt;%26lt;'\n';
Reply:cout%26lt;%26lt;"X="%26lt;%26lt;'\n';


I need help with this c++ program?

#include %26lt;conio.h%26gt;// needed for getch


#include %26lt;iostream.h%26gt;//needed for cout message





void startup();


void method();


int main()


{


startup();


method();








getch();


return 0;


}


void startup()


{


cout%26lt;%26lt;"safi khan"%26lt;%26lt;'\n';


cout%26lt;%26lt;"november 16, 2007"%26lt;%26lt;'\n';


cout%26lt;%26lt;"pg e"%26lt;%26lt;'\n';


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


}


void method()


{


int minimum = 0, maximum = 100, guess, press, press2;


cout%26lt;%26lt;"Think of a number."%26lt;%26lt;'\n';


cout%26lt;%26lt;"If done press 1: ";


cin%26gt;%26gt;press;





if (press = 1)


{


while (press2 == 3)


{


guess = maximum - minimum/2;


cout%26lt;%26lt; "Is this your guess "%26lt;%26lt;guess%26lt;%26lt;'\n';


cout%26lt;%26lt;"press 1) if too high press 2) if too low press 3) if i'm right press 4) to quit";


cin%26gt;%26gt;press;





if(press2 == 1)


{


maximum = guess, guess = (maximum - minimum) / 2;


continue;


}


if(press2 ==1)


{


minimum = guess, guess = (maximum - minimum) / 2;


continue;


}


if(press2 == 3)


{


cout%26lt;%26lt;"I'm correct. Game over.";


break;


}


if(press2 ==4)


{


cout%26lt;%26lt; "Game over";


break;


}





}


}


}

I need help with this c++ program?
It does work. The problem is that immediately after that, namely the statement





while (press2 == 3)





You never read anything into press2 nor do you initialize it to anything so it will never be 3. Consequently you exit out of the while loop and the enclosing if statement.


Strings and character arrays in C++?

I currently am having a problem regarding strings and character arrays. What I want to do is have a user type a string like "Johnson 5000" (using cin, not from an input file or preset string), and I want to store this input into two parallel arrays. One for characters while the other is for values. Assume that all variables are properly declared.





for (int canidateNum = 0; canidateNum %26lt; ROWS; canidateNum++)


{


cout %26lt;%26lt; "Canidate #" %26lt;%26lt; canidateNum + 1 %26lt;%26lt; ": ";


cin %26gt;%26gt; name[canidateNum] %26gt;%26gt; voteNum[canidateNum];


}


cout %26lt;%26lt; name[0] %26lt;%26lt; " " %26lt;%26lt; voteNum[0] %26lt;%26lt; endl;


...





However, upon entering something like "Johnson 5000" in the program, it will output the wrong result, displaying J and not 5000. I tried using various methods of inputing a string into a character array, but to no avail. I tried multi-dimensional arrays, but that didn't work for me either (probably did it wrong).





Can anyone assist me in this?

Strings and character arrays in C++?
Your array should be a string array.


A character array will only hold 1 character at each location.


Given that you are just storing the input in one location it is not surprising that it does not work.


If you still want a character array then it must have 2 dimensions. One for each candidate and one for each character of the input.


You then need to read your input one character at a time.
Reply:cout %26lt;%26lt; a[0]; would mean output the single character at the zero position.


cout %26lt;%26lt; a; should output the whole string.





char name[50];


cin %26gt;%26gt; name;


cout %26lt;%26lt; "Hello " %26lt;%26lt; name;





A string is an array of characters so if you want an array of strings your going to need an array of character arrays. array of array. Now I'm confused. Thank you.
Reply:How you declare your Arrays? correct way to declare an Array is





char name[20]; //string array to hold a name of 21 characters





or


char name[10][20]; /*2D string array to hold 11 names of 21 characters each/*





//to store names use use for loop


for(i=0;i%26lt;=10;i++)


cin%26gt;%26gt;name[i][];
Reply:Use a structure


typedef struct


{


char name[80];


long votes;


} candidate_type;





candidate_type candidate[ROWS];


for (int canidateNum = 0; canidateNum %26lt; ROWS; canidateNum++)


{


cout %26lt;%26lt; "Canidate #" %26lt;%26lt; canidateNum + 1 %26lt;%26lt; ": ";


cin %26gt;%26gt; candidate[canidateNum].name;


cin %26gt;%26gt; candidate[canidateNum].votes;


}


cout %26lt;%26lt; candidate[0].name %26lt;%26lt; " " %26lt;%26lt; candidate0].votes %26lt;%26lt; endl;


What is wrong with this C++ program?

#include%26lt;iostream%26gt;


using namespace std;





int main()


{


int num, bigNum, power, count;





cout %26lt;%26lt; "Enter an integer: ";


cin %26gt;%26gt; num;


cout %26lt;%26lt; "What power do you want it raised to? ";


cin %26gt;%26gt; power;


bigNum = num;


while (count++ %26lt; power);


bigNum *= num;


cout %26lt;%26lt; "The result is " %26lt;%26lt; bigNum %26lt;%26lt; endl;


return 0;


}

What is wrong with this C++ program?
count = 1 ; // probably count contains some junk value
Reply:Here are some of my few corrections with your program:


1.) Library header should have a ".h" extension


2.) avoid placing "int" in the main part of the program


3.) When you try to increment or decrement a variable, make sure that the variable already contains a value like 0 or 1.


4.)While loop's proper way of writing is while(condition){//statements}.


5.) A rule of thumb. Even if main() is a function, do not let main() return a value. Create another function to do the returning task.


6.) Put your "cout%26lt;%26lt;"The result is "%26lt;%26lt;bigNum%26lt;%26lt;endl;" outside the while statement so that it will not print the output while it is on the process mode. If you place the statement inside the while statement, the ways of reaching the result will be printed as well.





Here is my code (Extracted from your codes):





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





main()


{


int num, bigNum = 1, power, count = 0;





cout %26lt;%26lt; "Enter an integer: ";


cin %26gt;%26gt; num;


cout %26lt;%26lt; "What power do you want it raised to? ";


cin %26gt;%26gt; power;


while (count %26lt; power)


{


bigNum *= num;


count++;


}


cout %26lt;%26lt; "The result is " %26lt;%26lt; bigNum %26lt;%26lt; endl;


}





Hope it helps!

funeral flowers

What is the reason for declaration syntax error or declaration terminated incorrectly in c++ in this code?

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


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


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


int findpos(int[] ,int,int);


main()


{


int a[50] ,item,n,index;


clrscr();


cout%26lt;%26lt;"How many elements do u want ot insertmax 50 ";


cin%26gt;%26gt;n;


cout%26lt;%26lt;"Enter the elements";


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


{


cin%26gt;%26gt;a[i];


}


char ch = 'y';


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


{


cout%26lt;%26lt;"Enter element to be inserted";


cin%26gt;%26gt;item;


if(n==50)


{


cout%26lt;%26lt;"Overflow";


exit(1);


}


index=findpos(a,n,item);


for(i=n;i%26gt;index;i--)


{


a[i]=a[i-1];


a[index]=item;


n+=1;


cout%26lt;%26lt;"Want to insert more element ";


cin%26gt;%26gt;ch;


}


cout%26lt;%26lt;"The array now is as shown below \n";


for(i=0;i%26lt;n;i++)


cout%26lt;%26lt;a[i]%26lt;%26lt;" ";


cout%26lt;%26lt;endl;


getch();


}





int findpos(int a[],int size,int item)


{


int pos;


if(item%26lt;a[0])


pos=0;


else


{


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


{


if(a[i]%26lt;=item%26amp;%26amp;item%26lt;a[i+1])


pos=i+1;


break;


}


}


if(i==size-1)


pos=size;


}


return pos;


}

What is the reason for declaration syntax error or declaration terminated incorrectly in c++ in this code?
#include%26lt;iostream.h%26gt;


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


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


int findpos(int[] ,int,int);


main()


{


int a[50] ,item,n,index;


clrscr();


cout%26lt;%26lt;"How many elements do u want ot insertmax 50 ";


cin%26gt;%26gt;n;


cout%26lt;%26lt;"Enter the elements";


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


{


cin%26gt;%26gt;a[i];


}


char ch = 'y';


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


{


cout%26lt;%26lt;"Enter element to be inserted";


cin%26gt;%26gt;item;


if(n==50)


{


cout%26lt;%26lt;"Overflow";


exit(1);


}


index=findpos(a,n,item);


for(i=n;i%26gt;index;i--)


{


a[i]=a[i-1];


}


a[index]=item;





n+=1;


cout%26lt;%26lt;"Want to insert more element ";


cin%26gt;%26gt;ch;


}


cout%26lt;%26lt;"The array now is as shown below \n";


for(i=0;i%26lt;n;i++)


cout%26lt;%26lt;a[i]%26lt;%26lt;" ";


cout%26lt;%26lt;endl;


getch();


}





int findpos(int a[],int size,int item)


{


int pos;


int i;


for(i=0;i%26lt;size;i++)


{


if(a[i]%26gt;item)


{


pos=i;


break;


}


}





if(i==size)


pos=size;





return pos;


}








Now I have corrected all the problems. Also u r function logic was incorrect. It was not working for if number was to be inserted in last position.


if(a[i]%26lt;=item%26amp;%26amp;item%26lt;a[i+1]) // if i==n-1 how to get a[i+1]


pos=i+1;





Rest it was fine , there were yping mistakes regarding the braces.





OM NAMAH SHIVAY


Need help creating Loops for C++?

I have this code for temperature check im just a begginer on this don't blame me.


Our proffesor wont give us the code for loops.


ahhmm mainly after the prossess is done i would like to input whether i pres Y to try agian or N to shut down the program ahhm pls help! im a die hard on this thing here the code i've started with.








#include %26lt;iostream%26gt;


using namespace std;


int main ()


{


int temperature, pressure;


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" ; }





else {


cout%26lt;%26lt;"Okey" ; }





system ("PAUSE");


return 0;





}

Need help creating Loops for C++?
sorry dont know c++ but here's a shot..





#include %26lt;iostream%26gt;


using namespace std;


int main ()


{


string Continue;


Continue="y";


while(Continue=="y") {


int temperature, pressure;


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" ; }





else {


cout%26lt;%26lt;"Okey" ; }





system ("PAUSE");





return 0;





}


cout%26lt;%26lt; "Would you like to continue: \n";


cin%26gt;%26gt;Continue;


}
Reply:#include %26lt;iostream%26gt;


using namespace std;


int main ()


{


int temperature, pressure;


char try;


hell:


{


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" ; }





else {


cout%26lt;%26lt;"Okey" ; }


}


system ("PAUSE");


cout%26lt;%26lt;"Want to try again? Y/N";


cin%26gt;%26gt;try;


if(try=='Y')


{goto hell();}


return 0;





}











--i'm not sure bout the syntax but that's the flow...
Reply:char answer;





cout%26lt;%26lt; "Would you like to test the temp: \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" ;


}


else


{


cout%26lt;%26lt;"Okey" ;


}


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


cin%26gt;%26gt;answer;


}//ends while loop





Remember, the key to loops, whatever condition got you into it, has to get you out of it.


Another C++ Question?

Sort and output an array of inputted characters, based on their number of occurrences (from highest to lowest). What's wrong with this code?





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





int main(void)


{


using namespace std;





char chars[]="abcdefghijklmnopqrstuvwxyz";


int i;


int count[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0...





char inp;





cin%26gt;%26gt;inp;





while(inp !='.')


{


for(i=0;i%26lt;26;++i)


{


if(inp==chars[i])


count[i]++;


}


cin%26gt;%26gt;inp;


}





int swap;


char swapchar;





for(i=0; i%26lt;26; ++i)


{


if (count[i] %26gt; count[i + 1])


{


swap = count[i];


count[i] = count[i + 1];


count[i + 1] = swap;





swapchar = chars[i];


chars[i] = chars[i + 1];


chars[i + 1] = swapchar;


}


}





cout%26lt;%26lt;"Character Count"%26lt;%26lt;endl;








for(i=0;i%26lt;26;++i)


{


if(count[i]!=0)


{


cout%26lt;%26lt;chars[i]%26lt;%26lt;" "%26lt;%26lt;count[i]%26lt;%26lt;endl;


}


}





system("pause");





return 0;


}

Another C++ Question?
You put your using statement inside main().
Reply:Well first of all your namespace position is incorrect... it should be after #include%26lt;fstream%26gt;, you don't need fstream since you are not doing any file streaming.





Second, check your braces... { and } i tried fixing your program and it doesnt work actually because of the errors... your swap or as we call it "Bubble sort" is ok... but change the limit from 26 to 25 since you'll be comparing the second to the last. Also, you'll have to put that in another loop since you need to do that 25 times again since you'll only be pushing one lowest number at a time... you should also try considering if there are equal times it appeared.





Note: I didn't fix it to work... i want you to handle that part. ^^, i'm just here to correct a few errors =)


What's wrong with my C++?

Whats wrong with my code? X-(





When I try to compile, I get:


help.cpp:5: error: expected unqualified-id before ‘{’ token








#include%26lt;iostream%26gt;


using namespace std;


int main(void);





{








double dnumber1 = 0.0;


double dnumber2 = 0.0;


double dnumber3 = 0.0;


double daverage = 0.0;





cout %26lt;%26lt; "Please enter 3 numbers: " %26lt;%26lt; endl;


cin %26gt;%26gt; dnumber1;


cin %26gt;%26gt; dnumber2;


cin %26gt;%26gt; dnumber3;





daverage = (dnumber1 + dnumber2 + dnumber3) / 3;





cout %26lt;%26lt; "The average of those 3 numbers is: " %26gt;%26gt; daverage %26lt;%26lt; endl;





system("pause");


return 0;





}

What's wrong with my C++?
Here's what I think might be the problem:





First, move your dnumber1-3 declarations to before int main().





Secondly, change the %26gt;%26gt; b/n "The average is..." and daverage to %26lt;%26lt;
Reply:Why the semicolon after "int main(void)"?
Reply:You can't have a semicolon after int main(void)





This line is also wrong:


cout %26lt;%26lt; "The average of those 3 numbers is: " %26gt;%26gt; daverage %26lt;%26lt; endl;





everything should be going this way %26lt;%26lt;

floral design

My c++ program has some error..so pl correct them... it is a program to count unique words and what these are?

#include%26lt;iostream%26gt; // Allows program to perform input and output


using std::cout; // program uses cout


using std::cin; // program uses cin





#include%26lt;fstream%26gt;


using std::ifstream;








int main() // function main begains program execution





{





ifstream in;


char w[2000][15];


int alphabet[26];


char ch;


int ind = 0. subind = 0;





// initialise the array with zeros





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


alphabet[i] = 0;





//Open the input file


in.open("Text.txt");





//Check the file opened successfully.


if(!in.is_open())


cout%26lt;%26lt;"Unable to open input file."%26lt;%26lt;endl;





// Read the input file character by character


while(in.get(ch))





// if the character is an alphabet, then append the word


if( (ch%26lt;91%26amp;%26amp;ch%26gt;64)||(ch%26lt;123%26amp;%26amp;ch%26gt;96) )





{


w[ind][subind]=ch;


subind++;


}


// end if





// If a space found, move onto a new word





if(ch==' ')


{


w[ind][subind] = '\0';


subind=0;


ind++;


w[ind][subind]='\0';


} // end fi





} // end while





// Find the cout of occurence of words which starts with a letter





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


alphabet[toupper(w[i][0] = 65]++;





int max=0 // to find the max





// Display results





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


if(alphabet[i]!=0)


{


// Display the cout of words that starts with a letter





cout%26lt;%26lt;"\n" %26lt;%26lt; static_cast %26lt;char%26gt; (i+65) %26lt;%26lt;"'s-"


%26lt;%26lt;alphabet[i]%26lt;%26lt;"--";





// Find for the max occurences of a letter


if (alphabet[i]%26gt;alphabet[max])


max=1;


// Display the words that starts with a specific letter





for (int j=0;j%26lt;ind;j+=)


if(toupper(w[j][0]==(i+65))


cout%26lt;%26lt;w[j]%26lt;%26lt;", ";


} // end if





// A conclusion comment of the program


cout%26lt;%26lt;"\n\nLetter with that began the most words was "


%26lt;%26lt;static_cast%26lt;char%26gt; (max+65);





// close the input file


in.close();


return 0; // indicate program executed successfully


} // end of function, main

My c++ program has some error..so pl correct them... it is a program to count unique words and what these are?
What is the output? I may be able to analyse depending upon the actual output.





I cant run it here as i do not have any C++ compiler out here


Whats wrong with my code? C++?

i'm sure it has something to do with the "cin" or "getline" functions i've been trying to solve this problem with storing a string with spaces and save it to a text file but i can't seem to get it. an insertion error or infinite loop happens.





char title[ ]="";


string msg;


string temp;


char txt[ ] = ".txt";


string tempz;


ofstream myfile, myfile2;


system("cls");


cout %26lt;%26lt; "Title of message: ";


cin %26gt;%26gt; title;


tempz=title;


cout %26lt;%26lt; endl%26lt;%26lt;"Enter message: ";


getline(cin,msg);


temp = msg;


strcat(txt,title);





myfile.open (title);


myfile2.open("outbox.txt",ios::app);


myfile %26lt;%26lt;tempz %26lt;%26lt; endl %26lt;%26lt; temp %26lt;%26lt; endl %26lt;%26lt; endl;


myfile2 %26lt;%26lt; tempz %26lt;%26lt; endl %26lt;%26lt; temp %26lt;%26lt; endl %26lt;%26lt; endl;


myfile.close();


myfile2.close();

Whats wrong with my code? C++?
I can tell you at least one problem with your code. strcat(txt,title). Take a good look: http://www.cppreference.com/stdstring/st... or http://www.cplusplus.com/reference/clibr...





If you don't know how to work with C strings, don't randomly guess with functions. Either learn how C strings or don't use them at all.





Look at how strcat works. It's an append. so strcat (txt,title) is like txt + title, not title+txt. Your order is wrong. However, you have a bigger problem. You don't have enough of a buffer to do the concatenation.





If you were to do strcat(title,txt) (this would be the correct order), you would run into a problem. The C string title has enough space only for title. You can't fit txt into it. You should allocate a buffer big enough for both title and txt.





WHat's really confusing is that this is a perfect reason to use a *C++* string. Actually, your whole logic is a bit messy. You have a bunch of random C strings, C++ strings, and unnecessary assignments. Might want to clean up that code there.
Reply:You're not allocating any memory for title. You need to do something like:


char *title = new char[20];


My code encrypted C++ small program is not working! help please!! i am just learning C++...?

i am getting three errors:


Error1error C2228: left of '.length' must have class/struct/union


Error2error C2228: left of '.substr' must have class/struct/union


Error3error C2228: left of '.replace' must have class/struct/union


I DON'T KNOW HOW TO FIX THEM! ANY MORE ERRORS?


this is my code:


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;





using namespace std;





int main()


{


//declare variables


int code = 0;


string currentChar = "";


int numChar = 0;


int subscript = 0;





//get code number


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


cin %26gt;%26gt; code;





//determine number of characters


numChar = static_cast%26lt;int%26gt;(code.length());





//replace all numbers with the character x


while (subscript %26lt; numChar)


{


currentChar = code.substr(substr, 1);


if (currentChar == "0 to 9")


{


code.replace(subscript, 1, "x");


numChar = numChar - 1;


}


else


subscript = subscript + 1;


//end if


} //end while

My code encrypted C++ small program is not working! help please!! i am just learning C++...?
The compile errors are because 'code' is an int, and you're trying to call its length, substring, and replace operations. As an int, of course, it doesn't have these. It looks like you want 'code' to be a string.





Some other comments:





if numChar is size_t, you don't need any cast:


numChar = static_cast%26lt;int%26gt;(code.length()...





Syntax of this: if (currentChar == "0 to 9")


is ok, since currentChar is a (poorly named) string, but I don't think it's doing what you want.





Not sure what you're trying to do with:


numChar = numChar - 1;





So, make 'code' a string, and you may be on your way to getting something working. Even with that, though, I think you're making this problem much harder than it needs to be. This will do what you want:





string s1;


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


getline(cin,s1);


for (string::iterator i = s1.begin(); i != s1.end(); i++) {


if (isdigit(*i)) *i = 'x';


}


cout %26lt;%26lt; s1 %26lt;%26lt; endl;





#include %26lt;cctype%26gt; for isdigit( ).





You say you're encoding by substituting x for digits, how do you decode?


Why my ouptput in my C++ program is not right? help me please!!!?

i ma doing functions to convert Fahrenheit temperature


Celsius and i am getting a zero why? this is my code:


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::setprecision;


using std::fixed;





//function prototypes


double getFahrenheit();


int getCelcius(double);


int main()


{


//declare varibles


int celcius = 0;


double fahrenheit = 0;





//functions calls


fahrenheit = getFahrenheit();


celcius = getCelcius(fahrenheit);





cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(0);


cout %26lt;%26lt; "Celsius temperature is: " %26lt;%26lt; celcius %26lt;%26lt; endl;





return 0;


} //end of main function





//*****function definitions*****





double getFahrenheit()


{


double fahrenheit = 0.0;


cout %26lt;%26lt; "Enter fahrenheit: " %26lt;%26lt; endl;


cin %26gt;%26gt; fahrenheit;


return fahrenheit;


}





//*****function definitions*****





int getCelcius(double fahrenheit)


{


int output = 0;


output = 5.0/9.0 * (fahrenheit - 32.0);


return output;


}

Why my ouptput in my C++ program is not right? help me please!!!?
I compiled it and it worked for me... You may consider doing this though:





change int getCelcius to double getCelcius
Reply:You have an integer function, not a floating point one.

chelsea flower show

Sigh...c++ programming stumper...?

all right here is the deal, how do i fix this program? %26gt;%26gt;%26gt;





//This program will ask user to input two integers.


//main will call a user-defined function "GetSum" to return their sum.


//main will also call a system predefined function "pow" to get the square of sum.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;


using namespace std;





void Getsum(int);





int main ( )


{


int num1 = 0, num2 = 0, sum = 0, square = 0;





cout %26lt;%26lt; "enter the first integer: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "enter the second integer: ";


cin %26gt;%26gt; num2;





GetSum(num1, num2);


square = pow(sum);





cout %26lt;%26lt; "Their sum is " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The square of sum is " %26lt;%26lt; square %26lt;%26lt; endl;





return 0;


}





void GetSum(int A, int B)


{


int S = A + B;


return;


}

Sigh...c++ programming stumper...?
You had some pretty simple errors...


1) You spelled the Getsum function prototype differently than you did in the function call


2) You are passing a different number of parameters from the function prototype than in the function


3)Where does int A, and Int B get a value from? If the program would have worked you would have added whatever was stored in memory from A and B, since you didn't initialize them.


4)Void functions DONT return anything





I came up with a few fixes listed below, the error that you are getting from the pow(sum); line is very easily fixed, simply use the correct preprocessor directive...cmath didn't seem to work for me.








//This program will ask user to input two integers.


//main will call a user-defined function "GetSum" to return their sum.


//main will also call a system predefined function "pow" to get the square of sum.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;





using namespace std;





void GetSum(int num1, int num2);





int main ( )


{


int num1 = 0, num2 = 0, sum = 0, square = 0;





cout %26lt;%26lt; "enter the first integer: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "enter the second integer: ";


cin %26gt;%26gt; num2;





GetSum(num1, num2);


//square = pow(sum);





cout %26lt;%26lt; "Their sum is " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The square of sum is " %26lt;%26lt; square %26lt;%26lt; endl;





return 0;


}





void GetSum(int num1, int num2)


{


int S = num1 + num2;


return;


}


********************


Since I did not know the correct pre-processor directive, I did a minor re-write of your code, now function (now an int returning function) will return the value S.





//This program will ask user to input two integers.


//main will call a user-defined function "GetSum" to return their sum.


//main will also call a system predefined function "pow" to get the square of sum.





#include%26lt;iostream%26gt;


#include%26lt;cmath%26gt;





using namespace std;





int GetSum(int num1, int num2);





int main ( )


{


int num1 = 0, num2 = 0, sum = 0, square = 0;





cout %26lt;%26lt; "enter the first integer: ";


cin %26gt;%26gt; num1;


cout %26lt;%26lt; "enter the second integer: ";


cin %26gt;%26gt; num2;





GetSum(num1, num2);


//square = pow(sum);


sum = GetSum(num1,num2);


cout %26lt;%26lt; "Their sum is " %26lt;%26lt; sum %26lt;%26lt; endl;


cout %26lt;%26lt; "The square of sum is " %26lt;%26lt; square %26lt;%26lt; endl;


system("pause");


return 0;


}





int GetSum(int num1, int num2)


{


int S = num1 + num2;


return S;


}
Reply:Glad I could help Report It

Reply:main changes to





...


cout %26lt;%26lt; "Their sum is " %26lt;%26lt; GetSum(num1,num2) %26lt;%26lt; endl;


...











void GetSum(int A, int B)


{


int S = A + B;


return;


}





changes to





int GetSum(int A, int B) {


return A + B;


}


In C++, how do you figure out an average using a "for" loop?

I need to create a program which allows the user to enter how many numbers they wish to average, and then prompts them to enter that many numbers. Then, it will take those numbers and average them. Any help?





Here's my code so far:





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


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


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


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





int main()


{ clrscr();


int i;


int a;


cout%26lt;%26lt;"Hello there! Please enter how many numbers you want to average!: ";


cin%26gt;%26gt;a;


clrscr();


int average;


int B;


int sum;


for (i=0; i%26lt;=a-1; i++)


{


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


cin%26gt;%26gt;B;


sum=0;


average=sum+i+B/a;


}


cout%26lt;%26lt;"Hit any key to continue...\n\n";


getch();


cout %26lt;%26lt; "Your average = "%26lt;%26lt;average%26lt;%26lt;" ";


getch();





return 0;





}

In C++, how do you figure out an average using a "for" loop?
What the crap do you think you're calculating here???





   sum=0;


   average=sum+i+B/a;





Sum is always zero, so it doesn't add anything... Also, you calculate an average by summing all the numbers AND counting them, and only divide the sum by the number at the end...
Reply:here you go. the modified program.





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


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


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


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





int main()


{ clrscr();


int i;


int a;


cout%26lt;%26lt;"Hello there! Please enter how many numbers you want to average!: ";


cin%26gt;%26gt;a;


clrscr();


int average;


int B;


int sum=0;


for (i=0; i%26lt;=a-1; i++)


{


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


cin%26gt;%26gt;B;


sum+=B;


}


average=sum/a;


cout%26lt;%26lt;"Hit any key to continue...\n\n";


getch();


cout %26lt;%26lt; "Your average = "%26lt;%26lt;average%26lt;%26lt;" ";


getch();





return 0;





}


C++ problem?

Why does this one give me an application error?





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


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


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





int i,a,exp;


int rek(int n);





main()


{


clrscr();


cout%26lt;%26lt;"Give in base: "; cin%26gt;%26gt;a;


cout%26lt;%26lt;"Give in exponent: "; cin%26gt;%26gt;exp;


cout%26lt;%26lt;"Result is "%26lt;%26lt;rek(exp);


getch();


return(0);


}





int rek(int n)


{


return((exp==0)?1:rek(n-1)*a);


}

C++ problem?
%26gt; return((exp==0)?1:rek(n-1)*a);


................^^^





Did you mean (n==0)? exp isn't shrinking, only 'n' is.





You probably hit a "stack overflow" because the recursive calls never stop.





This error is likely due to the fact that you're referencing other global variables, which is bad practice and hides the dependency.


C++ Help plz?

What do I need to change when I paste this. I get problems for these two





#include %26lt;iostream%26gt;


using namespace std;





.........................................


#include %26lt;iostream%26gt;


using namespace std;


int factor(int a, int b);





int main()


{


int x, y;





cout %26lt;%26lt; "This program allows calculating the factor\n";


cout %26lt;%26lt; "Value 1: ";


cin %26gt;%26gt; x;


cout %26lt;%26lt; "Value 2: ";


cin %26gt;%26gt; y;





cout %26lt;%26lt; "\nThe Greatest Common factor of "


%26lt;%26lt; x %26lt;%26lt; " and " %26lt;%26lt; y %26lt;%26lt; " is " %26lt;%26lt; factor(x, y) %26lt;%26lt; endl;





return 0;


}





int factor(int a, int b)


{


while( 1 )


{


a = a / b;


if( a == 0 )


return b;


b = b / a;


if( b == 0 )


return a;


}


}

C++ Help plz?
i guess it should be #include%26lt;iostream.h%26gt;
Reply:i think you cant see your result.


try this, at first :


#include%26lt;iostream%26gt;


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





and before return 0, in main type:


getche ();





enjoy...

apple

C++ help! why a number 1 keeps on popping up in my priming read?

i am putting the 1 in my acummulator....but i don't know...i am just learning...can someone please help me! this is my code:


(by the way, what else do you all see wrong with it?)





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::fixed;


using std::setprecision;








int main()


{


//declare variables


double monthSales = 0.0;


int regions = 1;


double totalSales = 0.0;





while (regions %26lt;= 4)


{


cout %26lt;%26lt; "Enter region: " %26lt;%26lt; regions;


cin %26gt;%26gt; monthSales;





totalSales = totalSales + monthSales;





regions = regions + 1;


//end while


}


cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);


cout %26lt;%26lt; "Total sales: " %26lt;%26lt; totalSales %26lt;%26lt; endl;


return 0;


} //end of main function

C++ help! why a number 1 keeps on popping up in my priming read?
cout %26lt;%26lt; "Enter region: " %26lt;%26lt; regions;





because of that ...





The program is OK ... but there's some changes will make shorter ...





instead of


writing the 4 using::std ....





u can write once : using namespace std;





so, u won't need to write "using::std " else...





and instead of the while loop ... it would be better to use a for loop ... like :





for(regions=1;regions%26lt;=4;regions++)


{


// write your code here without the "regions = regions + 1" line ...


}





and instead of "totalSales = totalSales + monthSales;"


you could write "totalSales += monthSales;"
Reply:Whats wrong with this? The only thing I'm not sure about is the line that reads:





cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);





everything else looks fine.
Reply:cout %26lt;%26lt; "Enter region: " %26lt;%26lt; regions;





Are you asking why this is outputting "Enter region: 1"?





Simply, it's because you're outputting the the variable region.





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





That's what you want. If this isn't what you're asking then I see nothing wrong with your program.


De Morgans Law in C++?

I am having some problems trying to compile this program can anyone help me out?








#include %26lt;iostream%26gt;


using std::cin;





int main()


{





// input range to scan values


cin %26gt;%26gt; lowerx;


cin %26gt;%26gt; upperx;





// loop over all combinations of x and y


for (int x = lowerx; x %26lt; upperx; x++)


{


for (int y = lowery; y %26lt; uppery; y++)


{


if (!(x%26lt;5)%26amp;%26amp;!(y%26gt;=7) != !((x%26lt;5)||(y%26gt;=7))) // test equivalence


{


printf("Expressions are not equivalent. x = %d, y = %d", x, y);


return;


}


}


}





printf("Expressions are equivalent over the specified range");


return;

De Morgans Law in C++?
are you retarded? Do you have Suleyman? ;)





you never declared any of your upper or lower variables





you need your returns to be return(0);





and you're missing a '}' after the last return.
Reply:Good answer! Would be best, if you didn't call him retarded, though.





Use a syntax checker that will show you on which line something is wrong. Report It

Reply:Do you have "lowery" and "uppery" defined?


C++ program to check whether the string is palindromic or not?

please check the code for a question "Write a program to check whether the given string is palindromic or not."


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


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





void main


{ char x[100];


char y[100];


int temp1,temp2;


int ans = 0;


cout %26lt;%26lt; "Enter the word ";


cin %26gt;%26gt; x;


cout %26lt;%26lt; "Enter the word ";


cin %26gt;%26gt; y;


temp1 = strlen(x);


temp2 = strlen(y);


for(int k = 0;k %26lt; temp1 ;k--)


{for(int j = temp2;j %26lt;= ......;j--)


{if(x[k] == j[k])


ans = 1;


continue;


else


ans = 0;


}}


if(ans == 1)


cout %26lt;%26lt; "The string is palindromic";


else


cout %26lt;%26lt; "The string is not palindromic";


getch();


}


please make the necessary corretions as i dont have a compiler please help!!!!!!!!

C++ program to check whether the string is palindromic or not?
#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





int main()


{


char str[100];





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


cin %26gt;%26gt; str;


int x = strlen(str)-1;


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


{


if (str[i] == str[x-i])


{


continue;


}


else


{


cout%26lt;%26lt;"Not a palidrome"%26lt;%26lt;endl;


return 0;


}


}





cout %26lt;%26lt; "Indeed Palidrome"%26lt;%26lt;endl;


return 0;


}
Reply:Your code is messed up and I don't really understand your logic, if you want to chec whether a string is palindromic, you need only enter one string, not 2.





if it helps, this is how I'd do it, although it is written in C, you should not have much trouble understanding it, specially if you use google.








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


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


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





void trimSpaces(char *buffer);





int main()


{


char buffer[1024];


int start_ctr, end_ctr;


int is_pal;





fgets(buffer, 1023, stdin);


trimSpaces(buffer);





printf("After trim: %s\n", buffer);





end_ctr = strlen(buffer) - 1;


start_ctr = 0;


is_pal = 1;





while( (start_ctr != end_ctr) %26amp;%26amp; (end_ctr %26gt; start_ctr) )


{


if( tolower(buffer[start_ctr]) != tolower(buffer[end_ctr]) )


{


is_pal = 0;


break;


}


start_ctr++;


end_ctr--;


}





if( is_pal )


{


printf("Palindromic, indeed :)\n");


}


else


{


printf("Not palindromic :(\n");


}





return 0;


}





void trimSpaces(char *buffer)


{


int i = 0, j = 0;





for(; buffer[i] != '\0'; i++ )


{


if( isalpha(buffer[i]) )


{


buffer[j] = buffer[i];


j++;


}


}





buffer[j] = '\0';


}








Anobody besides me thinks yahoo needs a CODE tag?
Reply:Hi I study in DPS Ahmedabad if you know me please contact me(i will give you the compiler). This Pogramme will definately work for palindrome check


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


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


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


void main()


{


clrscr();


char x[100];


char y[100];


int temp1,temp2=0;


cout %26lt;%26lt; "Enter the word ";


cin %26gt;%26gt; x;


temp1 = strlen(x)-1;


for(int j= 0;j%26lt;strlen(x);j++)


{


y[j] = x[temp1 - j];


}


y[j] = '\0';


x[j] = '\0';


for(int k =0; k%26lt;strlen(x); k++)


{


if (x[k]==y[k]){


temp2 = temp2 + 1;


}


}


if (strlen(x) == temp2)


{cout%26lt;%26lt;"Palindrome";


}


else


{cout%26lt;%26lt;"not a palindrome";


}


getch();


}


C++ help needed please. Can you tell me how to fix it? thanks?

Use a one-dimensional array to solve the following problem:


A company pays its salespeople on a commission basis.


The salespeople each receive $200 per week plus 9 percent


of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $5000, or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson's salary is truncated to an integer amount):


$200$299


$300$399


$400$499


$500$599


$600$699


$700$799


$800$899


$900$999


$1000 and over


I have


# include %26lt;iostream%26gt;


using std::cin;


using std::cout;


using std::endl;


int main()


{ int gross, pay, frequency[10]={0};


cout%26lt;%26lt;"Enter salesperson's gross sales in dollars (-1 to end): "%26lt;%26lt;endl;


cin%26gt;%26gt;gross;


pay=200+.09*gross;


while ( gross!= -1)


{int y=(pay-200)/100;


frequency[y]++;


** I will continue on the details**

C++ help needed please. Can you tell me how to fix it? thanks?
Of course, you set y = (pay-200)/100. If you do the math, that means pay = 200 + (.09*20000) = 2000. So pay = 2000. Alright so y = (2000 - 200)/100 = 18. Your array frequency only goes to [10]. So you're writing over memory that isn't yours. Hence, the error. You can only go from frenquency[0]-[10]. Not to [18].

augustifolia

Need helpunderstanding what this c++ program does.?

char ch;


int ecount=0, vowels=0, other=0;


cin.get(ch);


while(!cin.eof())


{ switch(ch)


{ case ‘e’:


ecount++;


case ‘a’: case ‘i’:


case ‘o’: case ‘u’:


vowels++;


break;


default:


other++;


}//end switch


cin.get(ch);


}//end while


cout %26lt;%26lt; ecount %26lt;%26lt; “ e’s, ” %26lt;%26lt; vowels %26lt;%26lt; “ vowels, and ”


%26lt;%26lt; other %26lt;%26lt; “ other characters. “ %26lt;%26lt; endl;

Need helpunderstanding what this c++ program does.?
It counts the number of e's, vowels, and other characters inputed and then displays the totals on the screen.
Reply:Looks like this would read in a characters until end of file is reached. It counts total number of letter "e"s, total vowels (including e's I think), and total non-vowels or "other characters".


Then it outputs the counts of each.
Reply:The user inputs a letter...the program keeps count of the # of 'e' that are entered with the variable ECOUNT.





VOWELS is the number of {a,e, i,o,u} are entered.





OTHER represents the number of other characters.





Example:


input:


a


e


e


b


c


u


e





output:


3 e's, 5 vowels, and 2 other characters.


Need help with this C++ program. Code provided. What is the solution to my problem?

I'm not sure what all errors I'm getting but I know the one with the equation called out in float, it tells me that asValue is undefined but with the one where it is defined in the float, I get these errors.





Call of nonfunction


Lvalue required


Call of nonfunction





I'm using Borland.








//Program assignment #4 Exercise 13 p.156 - Donald Black


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


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


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





void main()


{


float actualValue, asValue, oneHundred, propertyTax;


cout %26lt;%26lt; "What is the actual value of your propert? \n";


cin %26gt;%26gt; actualValue;


actualValue(.6) = asValue;


cout %26lt;%26lt; "Your property's assessment value is: " %26lt;%26lt; asValue;


propertyTax = asvalue(.0064)


cout %26lt;%26lt; "Your property tax is: \n";


cin %26gt;%26gt; propertyTax;


system ("pause");


}

Need help with this C++ program. Code provided. What is the solution to my problem?
I'm using Dev C++ but the code should be about the same.





1) You are missing a "using namespace std;" statement


2) Change "void main()" to "int main()"


3) If you are multiplying values, use a * not parentheses


4) You asValue variable is case sensetive, so this needs to be changed in your propertyTax = asValue * .0064; statement.





The new version of C++ calls for headers in the format of #include %26lt;iostream%26gt; there is no need for the .h (in Dev C++ anyway--if yours is different, you need only change it back). Also, there is no need for the conio include in this program.





Try this:





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


using namespace std;


int main()


{


float actualValue, asValue, oneHundred, propertyTax;


cout %26lt;%26lt; "What is the actual value of your property? \n";


cin %26gt;%26gt; actualValue;


asValue = actualValue*.6;


cout %26lt;%26lt; "Your property's assessment value is: " %26lt;%26lt; asValue;


propertyTax = asValue*.0064;


cout %26lt;%26lt; "Your property tax is: \n";


cin %26gt;%26gt; propertyTax;


system ("pause");


}








Happy to see someone learning C++. I took it last semester; you will learn quickly.





Good luck


Another question about my c++ program i dont know what wrong with it?

i dont know what do cout at the end so i just couted amount i think its wrong. plzzz help.


#include %26lt;conio.h%26gt;// needed for getch


#include %26lt;iostream.h%26gt;//needed for cout and cin messages


#include %26lt;string.h%26gt;//needed for string copy


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


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


void startup();


void method();


int main()


{


startup();


method();


getch();


return 0;


}





void startup()


{


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


cout%26lt;%26lt;"jan 14, 2008"%26lt;%26lt;'\n';


cout%26lt;%26lt;"pg pro"%26lt;%26lt;'\n';


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


}


void method()


{


float amount, quaters=0, dimes=0, pennies=0, nickels=0;








cout%26lt;%26lt;"Enter amount of money less than $1: $ ";


cin%26gt;%26gt;amount;





quaters = amount / 25;


amount = quaters * 25;





dimes = amount / 10;


amount = dimes * 10;





pennies = amount / 1;


amount = pennies * 1;





nickels = amount / 5;


amount = nickels * 5;





cout%26lt;%26lt;"Your amount divided is: "%26lt;%26lt;amount%26lt;%26lt;'\n';


}

Another question about my c++ program i dont know what wrong with it?
If you are requesting the amount in the form of 0-99 you should enter it into an integer. You can then use the mod operator to accomplish what you need.





quarters=amount/25;


amount=amount%25;





dimes=amount/10;


amount=amount%10;





etc.
Reply:So that we can know what you are trying to do, please put comments in your program. Also please tell us what you are entering and what is coming out. THe way it is written, what comes out should be the same as what you put in, since you restore "amount" over and over.


-------------


OK, since you want to print out the number of quarters, etc, then print out the quarters, not the amount. In other words,





cout %26lt;%26lt; "The number of quarters is " %26lt;%26lt; quarters%26lt;%26lt;"\n";





and so on for the other types of coins.


Explain me this clearly? C++ code.?

#include %26lt;iostream%26gt;


using namespace std;


#define MAX 20





main(void)


{


int loop;


int hold[MAX];


int counter = 0;


char more;





while(1){


cout %26lt;%26lt; "enter a num" ;


cin %26gt;%26gt; hold[counter]; \\explain me this. Why was counter but in an array form[] ********************


cout %26lt;%26lt; "\nENTER MORE NUM (Y,N)";


cin %26gt;%26gt; more;


if(more == 'n'){


break;


}else{


counter++;


}





}


for(loop = 0; loop %26lt;= counter; loop++){


cout %26lt;%26lt; "element in" %26lt;%26lt; counter+1 %26lt;%26lt; " is " %26lt;%26lt; hold[loop]; \\ why was loop in a array[] in hold.


}


system("PAUSE");


return(0);





}

Explain me this clearly? C++ code.?
cin %26gt;%26gt; hold[counter]; \\explain me this. Why was counter but in an array form[]





this is assigning the stuff you are typing into the hold array variable at the index of "counter" (which is incrementing each time the while loop iterates)





2. cout %26lt;%26lt; "element in" %26lt;%26lt; counter+1 %26lt;%26lt; " is " %26lt;%26lt; hold[loop]; \\why was loop put in a array form inside hold.





this is outputting the hold array at the index of "loop" while the for loop increments loop until loop is less than or equal to the counter you generated in the while loop.





the while loop iterates until you Type N. As it loops it keeps incrementing "counter" up and indexing the hold array by "counter" for your input to be stored in...





hold[0] = "your input here"


hold[1] = "your second input here"





then the for loop takes the counter and uses it as it's maximum and loops using "loop" as the iterator and as the index for the hold array.





printing out "your input here"


then "your second input here"











make sense?
Reply:this program accepts integer in an array hold which can hold maximum of 20 elements. counter is used as index which is incremented each time when there are more elements to enter. when finished the loop exits. the next for loop is used to display the elements entered. Always the arrays are used with there indexes which shows the elements offset. hold[0],hold[1]....hold[20] are the elements if array hold.
Reply:Someone else has already explained the code.





What I would consider more appropriate for C++:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;vector%26gt;


using namespace std;





int main()


{





vector%26lt;int%26gt; nums;


string answer = "y";


int number = 0;


while(answer == "y")


{


cout %26lt;%26lt; "Enter a number".


cin %26gt;%26gt; number; nums.push_back(number);


cout %26lt;%26lt; "Another number (y/n) ?"


cin %26gt;%26gt; answer;


}





for(int i = 0; i %26lt; nums.size(); ++i)


{


cout %26lt;%26lt; "Element in " %26lt;%26lt; i %26lt;%26lt; " is " %26lt;%26lt; nums[i] %26lt;%26lt; endl;


}





return 0;





}

nobile

Fixing an issue in C++?

Can you tell me what it means by undeclared identifier thanks





#include %26lt;iostream%26gt;


#include "stdafx.h"


int main()


{


int apples; //amount of apples


int oranges; //amount of oranges


float total; //amount owed


float applespecial; // price of the apples


float orangeprice; // price of the oranges


float discount; //amount of the discount


cout %26lt;%26lt; "how many apples?" ;


cin %26gt;%26gt; apples ;


cout %26lt;%26lt; "how many oranges? Between one and one hundred";


cin %26gt;%26gt; oranges;


applespecial=int((apples+1)/2);


orangeprice=oranges*1.5; //warning C4244 '='


total=applespecial+orangeprice;


discount=total*(oranges/100);


total=total-discount;


cout %26lt;%26lt; "your total is : " %26lt;%26lt; total;


return 0;





}

Fixing an issue in C++?
I would recommend copying/pasting the entire error you go. It should tell you what identifier is undeclared.
Reply:orangeprice=float(oranges)*1.5;





it will solve
Reply:I'm not sure of this answer. You don't have a function. Why do you have a 'Return 0' at the end? Remove that statement and recompile and try.


C++ help???

i want to fix the errpr and make the program run


#include%26lt;iostream%26gt;


using namespace std;


double sum( double * begin, double * end) {


double *p = begin;


double result = 0;


while (p != end) {


result += *p++;


}


return result;


}


int main()


{


double *first,*last;


cout%26lt;%26lt;"enter the begin of the array \n";


cin%26gt;%26gt;* first;


cout%26lt;%26lt;"enter the last item in the array \n";


cin%26gt;%26gt;* last;


cout%26lt;%26lt;sum(* first,* last);


system("pause");


return 0;


}

C++ help???
#include%26lt;iostream%26gt;


using namespace std;


double sum( double * begin, double * end) {


double *p = begin;


double result = 0;


while (result %26lt; *end) { // %26lt; end


result += *p+result;


}


return result;


}





int main()


{


double first,last;


cout%26lt;%26lt;"enter the begin of the array \n";


cin%26gt;%26gt;first;


cout%26lt;%26lt;"enter the last item in the array \n";


cin%26gt;%26gt;last;


cout%26lt;%26lt;sum(%26amp;first,%26amp;last);


system("pause");


return 0;


}


C++, lowest test score drop question?

Writing a program where user inputs 5 scores %26amp; lowest score drops. Has to be in 2 seperate functions. I have getValue function done ok; user inputs 5 scores as array of type float. When I move on to the second function (findLowest), I don't know how to transfer the scores found in getValues. No global variables, %26amp; I think I'm supposed to use call by reference, but I'm confused at how you would take the array from getValues and do a call by reference to get the scores into findLowest.





# include %26lt;iostream%26gt;


using namespace std;





void getValues();


void findLowest();








int main()


{


getValues();


findLowest();


return 0;


}





void getValues()


{


const int size = 5;





float score[size];





cout%26lt;%26lt; "Enter " %26lt;%26lt; size%26lt;%26lt; " scores, seperated with a space: \n";


cin%26gt;%26gt; score[0];





findLowest();


}





void findLowest()


{


int i;


float lowscore;





lowscore = score[0];


for (i=1; i%26lt;5; i++)


{


cin%26gt;%26gt;score[i];


if (score[i] %26lt; lowscore)


lowscore = score[i];


}

C++, lowest test score drop question?
As soon as getValues() returns, the score array gets deleted. What you need to do is create an array in the main() method (this isn't a global variable) and pass it to getValues() and findLowest() (c++ will automatically pass arrays by value).





int main()


{


float highscores[4];


getValues(score);


return 0;


}





void getValues(float[ ] highscores)


{


float scores[5];





// Populate the the scores array


// i.e. scores[ i ] = input





float lowscore = findLowest(scores);





//move high scores into highscores array


int count=0;


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


{


if (scores[i] != lowscore) // as long as it's not the low score


highscores[count++] = scores[i];


}





}








float findLowest(float[ ] scores)


{


// Run your algorithm to find the lowest score in scores[ ]


// return that score


}





That's how it's done. Just fill in the blanks. Good luck!


Need helpunderstanding what this c++ program does.?

char ch;


int ecount=0, vowels=0, other=0;


cin.get(ch);


while(!cin.eof())


{ switch(ch)


{ case ‘e’:


ecount++;


case ‘a’: case ‘i’:


case ‘o’: case ‘u’:


vowels++;


break;


default:


other++;


}//end switch


cin.get(ch);


}//end while


cout %26lt;%26lt; ecount %26lt;%26lt; “ e’s, ” %26lt;%26lt; vowels %26lt;%26lt; “ vowels, and ”


%26lt;%26lt; other %26lt;%26lt; “ other characters. “ %26lt;%26lt; endl;

Need helpunderstanding what this c++ program does.?
counts the number of e, vowels, and rest...duh you newbie

hibiscus

C++ help! when i build the solution and try to test it,....?

the output disappear without letting me check if it's right or not. how can i make it to stay? i am usin microsoft visual studio 2005.





this is my code:





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::fixed;


using std::setprecision;





int main()


{


//declare variables


int registrants = 0;


int fee = 0;





//enter input


cout %26lt;%26lt; "Enter number of registrants: ";


cin %26gt;%26gt; registrants;





//calculate and display order price





cout %26lt;%26lt; fixed %26lt;%26lt; setprecision(2);





if (registrants %26gt; 0)


{


if (registrants %26gt; 1 %26amp;%26amp; registrants %26lt;= 4)


cout %26lt;%26lt; "Seminar Fee: $" %26lt;%26lt; registrants * 100 %26lt;%26lt; endl;


else if (registrants %26gt;= 5 %26amp;%26amp; registrants %26lt;= 10)


cout %26lt;%26lt; "Seminar Fee: $" %26lt;%26lt; registrants * 80 %26lt;%26lt; endl;


else if (registrants %26gt; 11)


cout %26lt;%26lt; "Seminar Fee: $" %26lt;%26lt; registrants * 60 %26lt;%26lt; endl;


//endifs


}


else


cout %26lt;%26lt; "Invalid Data" %26lt;%26lt;endl;


return 0;


} //end of main function

C++ help! when i build the solution and try to test it,....?
The common solutions seem to be using getch() or system("pause") in the correct place to hold the terminal window open while you examine the output.





If you are not just a Windows drone, a more portable solution is to include and call a function something like the below at the proper point(s) in your program.





// A simple debugging routine to use with DOS console apps to hold the DOS box open


void HoldConsole(void)


{


char hold[3];





cout %26lt;%26lt; "\nHit %26lt;Enter%26gt; ";


cin.getline(hold, sizeof(hold), '\n');


cout %26lt;%26lt; endl;


}
Reply:Put in some wait-for-keyboard-input code to pause the execution.





Hope that helps.
Reply:insert the statement





getch();





one line before return 0; ... and don't forget to include the library conio.h