Monday, May 24, 2010

A C++ Question ? How to read a 2 letter character in an input.?

I have to create a program that reads in characters until the user enters the correct two- character sequence= (cs) to open door.





Sample Data:


cs = Door opens





ccs=Door opens





fkcds=Door doesn't open





kasfcsgd=Door opens





I have to use While Loop. but I need Answers what to put in -





My code:


/*====================================...





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





void main()


{


char ch;


int done;





cout %26lt;%26lt; endl %26lt;%26lt; "You have before you a closed door." %26lt;%26lt; endl;





while(done)


{


cout%26lt;%26lt;"Enter a sequence of letters: ";


cin%26gt;%26gt;ch;





if()


}





cout %26lt;%26lt; "The door opens. Congratulations!" %26lt;%26lt; endl;


}





/*====================================...





It is not Finished, It is just a shell I made to help.

A C++ Question ? How to read a 2 letter character in an input.?
You have to keep track of the previous characters. It depends a little bit if you want the user to have to hit enter first or you want to know right when the correct keys are pressed.





If hitting characters then enter is what you want:


#include %26lt;string%26gt;





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


string in;


cin %26gt;%26gt; in;


if (in.find("cs") != string::npos)


{


// found it


}








If you want to do something without waiting for enter:





cout%26lt;%26lt;"Enter a sequence of letters: ";


char prev = 0, curr = 0;


while(prev != 'c' || curr != 's')


{


prev = curr;


curr = getch();


}


//when loop exits, prev == 'c' and curr == 's'





You can also do it with string class. this is nice as the length of the check increases.





cout%26lt;%26lt;"Enter a sequence of letters: ";


string in;


while(in != "cs")


{


char ch = getch();


in = in.substr(1); // trims off left character


in.push_back(ch)


}
Reply:after #include%26lt;iostream.h%26gt;


put #include%26lt;string.h%26gt; or #include%26lt;string%26gt; (depends on the version)





Then to add a string variable just put:





string dooropen;





(dooropen is an example)





string is a variable like any other





PS What program do you use for C++ coding...i'm trying to find one...email me...


Simple c++ program. help needed/?

Hey guys, i typed this simple code to see if my compiler was running properly but im getting some errors i cant solve. plz help...





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


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


class A


{


private: int a, b;


public:


void do()


{


cout%26lt;%26lt;"\n"%26lt;%26lt;"enter first no. : ";


cin%26gt;%26gt;a;


cout%26lt;%26lt;"\n"%26lt;%26lt;"enter 2nd no. :";


cin%26gt;%26gt;b;


cout%26lt;%26lt;"\n"%26lt;%26lt;"sum = "%26lt;%26lt;a+b;


}


};


int main()


{


clrscr();


A ob;


ob.do();


getch();


return 0;


}

Simple c++ program. help needed/?
do is a keyword, change function name to something else
Reply:What are the errors?





http://catb.org/~esr/faqs/smart-question...


Whats wrong with this C++ program??????

#include %26lt;iostream%26gt;


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





int();


{


std::cout %26lt;%26lt; "add two numbers....The first one will be subtracted from the second" %26lt;%26lt; std::endl;


int v1, v2;


std::cout %26lt;%26lt; "The no: to be sbtracted" %26lt;%26lt; std::endl;


std::cin %26gt;%26gt; v1;


std::cout %26lt;%26lt;"The no: to subtract from" %26lt;%26lt; std::endl;


std::cin %26gt;%26gt; v2;


std::cout %26lt;%26lt;"The difference between " %26lt;%26lt; v1 %26lt;%26lt;"and" %26lt;%26lt; v2 %26lt;%26lt; "is" %26lt;%26lt; v2-v1 %26lt;%26lt; std::endl;





return 0;


}





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





Please re do the program if you find the error.


Its a subtraction

Whats wrong with this C++ program??????
I think the first line has to be 'int main()'





ie add 'main' and lose the terminating ';'. The bit between the braces is defining 'main', so we don't want to terminate before we reach it!
Reply:hahaha simple mistake Report It



Whats wrong with this c++ program?

the answerr that always comes out is one.





class alvarez{


protected:


double Inter;


double time;


double percentage;


public:


void CalcArea();


void ShowArea();


alvarez(double I=0, double t=0);


};


alvarez::alvarez(double I,double t)


{


I=Inter;


t=time;


}


void alvarez::CalcArea()


{


percentage= Inter/time;


}


void alvarez::ShowArea()


{


cout %26lt;%26lt;"the percentage is:"%26lt;%26lt; percentage %26lt;%26lt;endl;


}





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


#include "alvarez.h"





class ThreeD: public alvarez


{


protected:





double Percentage;





public:


void CalcVol();


void ShowVol();


ThreeD (double I=0, double t=0);


};





ThreeD :: ThreeD(double I, double t):alvarez(I,t)


{





}


void ThreeD::CalcVol(){


Percentage=Inter/time;


}


void ThreeD::ShowVol(){


cout %26lt;%26lt; "The percentage is: " %26lt;%26lt; Percentage %26lt;%26lt; endl;


}





main(){


double I,t;


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


cin %26gt;%26gt; I;


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


cin %26gt;%26gt; t;


ThreeD box (I,t);


box.CalcVol();


box.ShowVol();


return(0);


}

Whats wrong with this c++ program?
The result from the above-stated codes will always be 1 because zero divided by zero is ONE. (0 / 0 = 1).





Take a better look at your constructor that takes in two arguments 'l' and 't'. It should be the integer variables 'Inter' and 'time' that receives the value of the arguments l and t and not vice versa...





This is how to solve it...





alvarez::alvarez(double l, double t)


{


Inter = I;


time = t;


}

kudzu

A C++ Question ? How to read a 2 letter character in an input.?

I have to create a program that reads in characters until the user enters the correct two- character sequence= (cs) to open door.





Sample Data:


cs = Door opens





ccs=Door opens





fkcds=Door doesn't open





kasfcsgd=Door opens





I have to use While Loop. but I need Answers what to put in -





My code:


/*====================================...





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





void main()


{


char ch;


int done;





cout %26lt;%26lt; endl %26lt;%26lt; "You have before you a closed door." %26lt;%26lt; endl;





while(done)


{


cout%26lt;%26lt;"Enter a sequence of letters: ";


cin%26gt;%26gt;ch;





if()


}





cout %26lt;%26lt; "The door opens. Congratulations!" %26lt;%26lt; endl;


}





/*====================================...





It is not Finished, It is just a shell I made to help.

A C++ Question ? How to read a 2 letter character in an input.?
just add another variable...one that keeps track of a "c" and one for an "s"...the better way is to use strcmp(), but be careful of hidden null characters at the end which may or may not be an issue.


Sending output to a file and screen in c++?

this is my program so far: i am trying to send and output to a file and also to the screen. i do not want the prompts to appear on the screen, therefore i need to send the prompts to a file. Some how it is going to a file but on the screen i still have to input the information.


can someone tell me please what i am doing wrong.





{


int custnum;


double $orderplaced, $orderpaid, balancedue;





ofstream outfile;


outfile.open("g:A3.out");





cout.setf(ios::fixed,ios::floatfield);


cout.precision(2);





cout %26lt;%26lt; "\t\tHandy Hardware Company's Customer Database" %26lt;%26lt; endl;


cout %26lt;%26lt; "\t\t___________________________________... " %26lt;%26lt; endl;


cout %26lt;%26lt; endl;


cout %26lt;%26lt; "\tOrders Placed($)\tOrders Paid($)\tBalance Due\tStatus " %26lt;%26lt; endl;


outfile %26lt;%26lt; "Enter the Customer Number " %26lt;%26lt; endl;


cin %26gt;%26gt; custnum;


while (custnum %26gt; 0) { // read unitl a negative number is entered


outfile %26lt;%26lt; "Enter dollar amount of Orders placed

Sending output to a file and screen in c++?
First of all, you have the prompt in the outfile, then you cin the answer:


outfile %26lt;%26lt; "Enter the Customer Number " %26lt;%26lt; endl;


cin %26gt;%26gt; custnum;


That's not user friendly, because you would have to be looking at the file prompts, then entering the answers on screen.





Also, I don't think you should put the prompts in the file. If you can't have them on the screen, don't put them anywhere, because when you go to read the file, you'll have to overlook the prompts. So if the prompts can't go on the screen, don't put them anywhere. Just infile%26gt;%26gt; your answers.
Reply:Well,


1.$ is an illegal character for a declaration (I think).


2.I think you mean "G:\\A3.out".


Hold on a second, I need to see the details again...


Oh, that's it? Well, you're doing OK so far. Remeber


cout is tied to stdout (the screen).


outfile is tied to file G:a3.out (do you mean G:\\A3.out? The double backslash is because of the backslash being an escape character.)


and cin is tied to stdin (the keyboard).


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.


C++: Help with putting an extracted number back together?

Here is my program that finds out if an inputted number is a palindrome or not:





#include %26lt;iostream%26gt;


using namespace std;





int main()


{


cout %26lt;%26lt; "Enter a five-digit integer: " %26lt;%26lt; endl;


int num;


cin %26gt;%26gt; num;





int fifth = num % 10;


num = num / 10;





int fourth = num % 10;


num = num / 10;





int third = num % 10;


num = num / 10;





int second = num % 10;


num = num / 10;





int first = num % 10;


num = num / 10;





if (first == fifth %26amp;%26amp; second == fourth)


cout %26lt;%26lt; num %26lt;%26lt; " is a palindrome" %26lt;%26lt; endl;


else


cout %26lt;%26lt; num %26lt;%26lt; " is not a palindrome" %26lt;%26lt; endl;





return 0;


}





All of my results are correct except for when I try to print out the original inputted number as a whole again. How do I do this? I keep getting 0.





I appreciate your help! ^_^

C++: Help with putting an extracted number back together?
You're storing the number in the "num" variable, but then you're repeatedly dividing num by 10 while determining whether or not it's a palindrome. In other words, you're destroying the value in num as you go along.





To fix this store a temporary copy of num in another variable (e.g. "original_num") at the start of the program and print that variable instead. Either that or print the value of num before you start changing it.





(BTW, it won't work if you try to restore the value of num by repeatedly multiplying it back out by 10 a few times. Every time you divide an integer variable the remainder is discarded.)
Reply:It is because





the original input getting change when you do the following





num = num / 10;





insted of that execute the following code it will work





#include %26lt;iostream%26gt;


using namespace std;





int main()


{


cout %26lt;%26lt; "Enter a five-digit integer: " %26lt;%26lt; endl;


int num,num1;


cin %26gt;%26gt; num;


num1=num;


int fifth = num % 10;


num = num / 10;





int fourth = num % 10;


num = num / 10;





int third = num % 10;


num = num / 10;





int second = num % 10;


num = num / 10;





int first = num % 10;


num = num / 10;





if (first == fifth %26amp;%26amp; second == fourth)


cout %26lt;%26lt; num1 %26lt;%26lt; " is a palindrome" %26lt;%26lt; endl;


else


cout %26lt;%26lt; num1 %26lt;%26lt; " is not a palindrome" %26lt;%26lt; endl;





return 0;


}
Reply:You are changing the original number as the program runs.


Save the original number in another variable and then print that out.

garland flower

C++ beginner help plz?

const int size = 1000;


int sortarray[size];


int key = 0, j = 0;





int main(){





srand(time(0));


for(int i = 0; i %26lt; size; i++){ // starts the for loop


sortarray[i] = (rand()%10000) + 1; // makes numbers between 1 through 20 %/returns remainder


cout%26lt;%26lt;sortarray[i]%26lt;%26lt;" , ";


}





for(int i = 0; i %26lt; size; i++){ // starts the bubble sort


for(int j = size - 1; j %26gt; i; j--){ // starts the comparisons at the end of the array


if(sortarray [j] %26lt; sortarray[j - 1]){ // checks the current element with the previous one


key = sortarray[j]; // switches the elements if the conditions are met


sortarray[j] = sortarray[j - 1];


sortarray[j - 1] = key;


}


}


}





// formats the output prints to screen


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





for(int i = 0; i %26lt; size; i++){ // prints the sorted array





cout%26lt;%26lt;sortarray[i]%26lt;%26lt;", ";


}


return 0;


}


I wrote this code for bubble sort. How do I add a third array to fit the requirements of exchange sort method?

C++ beginner help plz?
Your code for bubble sort looks correct, but your question is confusing. Bubble sort is an exchange sort. Exchange sort is a general class of sorting algorithms, where the sort is executed by exchanging or swapping list elements.





I don't know what you mean by a third array (as you only have 1 array so far, the one to sort).
Reply:I suggest you to join C/C++ programming groups in yahoo for your programming doubts...


C++ question 10 POINTS COMIN YOUR WAY!?

is there a way to write a command like:





do


{


//stuff here


}while(variable != char)





is there a way to something like that??


i want to do a set of commands if the variable is char NOT int


please help!


code:





if(userNumber == 0, 40000000)


{


cout %26lt;%26lt; userNumber %26lt;%26lt; " is my favorite number too!" %26lt;%26lt; endl;


system("pause");


return 0;


}


do


{


int userNumber;


cout %26lt;%26lt; "Hey! That's not a real number. Please enter your favorite number and then press enter." %26lt;%26lt; endl;


cin %26gt;%26gt; userNumber;


system("pause");


do


{


cout %26lt;%26lt; userNumber %26lt;%26lt; " is my favorite number too!" %26lt;%26lt; endl;


system("pause");


return 0;


}while(userNumber == 0, 40000000);





}while(char);


}








i conjured this out of no where LOL


this isn't similiar to my code but it sort of is


(i changed sentences variables and removed other code as well)


thanks in advance

C++ question 10 POINTS COMIN YOUR WAY!?
I'll try this code.. then I'll tell you something!


C++ send a remote command?

Using winsock, I am trying to make a server that you can send a command to, and it will execute that command. For example, if you connect to it using telnet and send the letter 'N', the notepad will open up. Here's some of the code:





SOCKET sock = (SOCKET)lpParam;


char szRecvBuff[1024],


szSendBuff[1024];


int ret;





while (true)


{


// Receive data


ret = recv(sock, szRecvBuff, 1024, 0);


// Check the recieved data


if (ret == 0)


break;


else if (ret == SOCKET_ERROR)


{


cout %26lt;%26lt; "Recieve data failed!" %26lt;%26lt; endl;


break;


}


szRecvBuff[ret] = '\0';


cout %26lt;%26lt; szRecvBuff %26lt;%26lt; endl;


while(true)


{


if (szRecvBuff == "N")


{


system("START notepad.exe");


cout %26lt;%26lt; "notepad.exe started" %26lt;%26lt; endl;


}


}





The received data ("N") appears in the console window, but the message "notepad.exe started" never shows up, and the notepad doesn't start up. What is the problem?

C++ send a remote command?
... first :





why you use a loop to check che command?





2nd .. you can't use the operator == to compare a buffer with another .. it's compare only the address of array and the result is false ..





use strcmp or szRecvBuff[0] == 'N' ...





3th .. use CreateProcess api to run a process under windows.. system isn't standard, and the function lock your code until the process run..


Help with a c++ program.?

i'm suppose to rewrite the findMAX() so that the variable MAX is declared in main (), is used to store the maximum value of two passed numbers.The value of MAX should be set directly from within findMAX().





so can someone help me finish this?





heres my code:








#include %26lt;iostream%26gt;


using namespace std;


int findMAX(int, int); // the funtion prototype


int main()





{





int firstnum, secnum, MAX;





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


cin %26gt;%26gt; firstnum;


cout %26lt;%26lt; "Great! Please enter a second number: ";


cin %26gt;%26gt; secnum;





MAX = findMAX (firstnum, secnum); // the function is called here





cout %26lt;%26lt; "\nThe maximum of the two numbers are :" %26lt;%26lt; MAX %26lt;%26lt; endl;


return 0;





}





// following is the function findMAX()





int findMAX(int x, int y)





{ //start of function body


int maxnum; //variable declaration





if (x %26gt;= y) //find the max number


maxnum = x;


else


maxnum = y;return maxnum;





cout %26lt;%26lt;"\nThe maximum of the two numbers entered are: " %26lt;%26lt; maxnum %26lt;%26lt; endl;





system( "pause" );


return 0;





}


// End of Program

Help with a c++ program.?
%26gt;:(





since i guess theres no way i can post another comment, so I guess i'll use my sisters account.





anyway so the code looks like this:








#include %26lt;iostream%26gt;


using namespace std;


int findMAX(int x, int y); // the funtion prototype


int main()





{








int firstnum, secnum, MAX;





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


cin %26gt;%26gt; firstnum;


cout %26lt;%26lt; "Great! Please enter a second number: ";


cin %26gt;%26gt; secnum;





MAX = findMAX (firstnum, secnum); // the function is called here





cout %26lt;%26lt; "\nThe maximum of the two numbers are :" %26lt;%26lt; MAX %26lt;%26lt; endl;


return 0;





}





// following is the function findMAX()





int findMAX(int x, int y)





{ //start of function body


int maxnum; //variable declaration





if (x %26gt;= y) //find the max number


maxnum = x;


else


maxnum = y;


return maxnum;





cout %26lt;%26lt;"\nTe maximum of the two numbers entered are: " %26lt;%26lt; maxnum %26lt;%26lt; endl;























system( "pause" );


return 0;





}


// End of Program








so is this right?
Reply:and if you want it even shorter, drop the redundant maxnum variable and use a somewhat more standard syntax.





int findMAX(int x, int y)


{


return x %26gt;= y ? x : y;


}
Reply:Your findmax() function should be











int findMAX(int x, int y)





{ //start of function body


int maxnum; //variable declaration





if (x %26gt;= y) //find the max number


maxnum = x;


else


maxnum = y;








return maxnum;





}


// End of Program








EDIT:





and if you want to make the function shorter:





int findMAX(int x, int y)





{





x %26gt;= y ? return x :return y;


}

blazing star

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;


}


Error C4430: missing type specifier - int assumed. Note: C++ does not support default-int?

#include "stdafx.h"


#include %26lt;iostream%26gt;


using namespace std;





main()


{


int h;





cout %26lt;%26lt; "Enter an age and press ENTER: \n" %26lt;%26lt; h;


cin %26gt;%26gt; h;





if (h %26lt;= 0 %26amp;%26amp; h %26gt;= 12)


cout %26lt;%26lt; "The subject is a child.\n" %26lt;%26lt; h;





else if (h %26lt;= 13 %26amp;%26amp; h %26gt;= 19)


cout %26lt;%26lt; "The subject is a teenager.\n" %26lt;%26lt; h;





else if (h %26lt;= 20 %26amp;%26amp; h %26gt;= 60)


cout %26lt;%26lt; "The subject is an adult.\n" %26lt;%26lt; h;





else if (h %26lt;= 61 %26amp;%26amp; h %26gt;= 100)


cout %26lt;%26lt; "The subject is an adult.\n" %26lt;%26lt; h;





else


cout %26lt;%26lt; "The subject is dead.\n" %26lt;%26lt; h;





return 0;


}





can someone help me so the error goes away?

Error C4430: missing type specifier - int assumed. Note: C++ does not support default-int?
main()


...should be...


int main(void)





...aside from that, you should probably work on understanding how "else if" works.


C++ help please?

im making a text editor please look at the code and tell me my problem thanks.





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


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


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


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


using namespace std;





int main()


{


system("@echo off");


cout %26lt;%26lt; "1. create a file \n 2. open file";


int choice;


cin %26gt;%26gt; choice;


switch (choice) {


case 1 :


int fname;


cout %26lt;%26lt; "filename and location.";


cin %26gt;%26gt; fname;


ofstream file;


file.open(fname);


file.close();


cout %26lt;%26lt; "how many lines do you want in the file?";


int lines;


cin %26gt;%26gt; lines;


if (lines = 1){


char lineA;


cin %26gt;%26gt; lineA;


}


if (lines = 2){


char lineA;


cin %26gt;%26gt; lineA;


char lineB;


cin %26gt;%26gt; lineB;


}


if (lines = 3){


char lineA;


char lineB;


char lineC;


cin %26gt;%26gt; lineA;


cin %26gt;%26gt; lineB;


cin %26gt;%26gt; lineC;


}


if (lines = 4){

C++ help please?
Here's one problem:


The if checks are assigning lines to a number instead of comparing lines to a value.





change " if(lines = 1) " to " if(lines == 1) "


and change that for every if check.


Please help in c++ I have this code with some mistakes help i don't have time!!!?

# include %26lt; iostream%26gt;


using namespace std ;


using std :: cout;


using std :: cin;


using std :: endl;





class student{


public:


bool ok;


bool woman;


bool instudy;


int finalchoice ;


int a;


bool success;


string name ;


int grade ;


int choice[9] ;





student(){


instudy=false;


ok= false ;


success = true ;


woman = true ;


finalchoice = 0;


a=1;


}





}





int calcu(int ,int ,bool ,int , student [] );











void main (){


student students[800];


int s[]={60,80,40,40,40,60,50,80,50};


int xx[9];


int yy[9];


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





cout%26lt;%26lt;"enter student name ";








cout%26lt;%26lt;" enter the student grade ";


cin%26gt;%26gt; students[i].grade;


cout%26lt;%26lt;" put the choices (1.......9)";


for(int j=0;j=9;j++){


cin%26gt;%26gt;students[i].choice[j];


}


}


int x=240,y=240;


for(int k=1;k%26lt;=9;k++){


int fcounter = 0; int mcounter=0;


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


if(students[i].choice[i]-1==k)





if(students[i].woman == true ){

Please help in c++ I have this code with some mistakes help i don't have time!!!?
You could increase the chance of your question being answered by being a lot more specific about the problem.. Compile time error, run-time error..? Asking people to copy and paste and into their c++ compilers and compiling and running it is asking a bit too much... =P





so what EXACTLY is the problem?
Reply:who is even gonna look at that?? what are you , sitting in an exam room??
Reply:May be you can contact a C++ expert at websites like http://getafreelnacer.com/

imperial

C++ question?

I need to make this program accept only numbers. No symbols or letters may be used. What can I set the inputNum[i] to that will allow only whole numbers to be inserted and accepted?





cout %26lt;%26lt; "\nPlease enter a whole number: ";


cin %26gt;%26gt; inputNum[i]; //will not function properly.


if (i != ??????????? %26lt;%26lt;%26lt;%26lt;%26lt;%26lt;------THIS IS MY PROBLEM


{


cout %26lt;%26lt; "\nThe number is valid: " %26lt;%26lt; endl;


}


else


{


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


exit (0);


}

C++ question?
No matter what you do, when you read the line in, it will be a string of characters. The numbers 0 - 9 are the ascii codes 048 - 057.





You need to check to make sure that each char in the string is between 48 and 57.





Your other option is to use the built in atoi() function in a try block. If it fails and you get to the catch block, you know it isn't a number.
Reply:By "whole", I assume you mean natural.





So... all you have to do is verify that each character is a digit:





std::string str;


std::cout%26lt;%26lt;"Enter a whole number: ";


std::cin%26gt;%26gt;str;





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


{


if( !(str[i] %26gt;= '0' %26amp;%26amp; str[i] %26lt;= '9') )


{


// Invalid input


exit(0);


}


}





// The input is valid
Reply:if u use an integer array, even if someone enters a real number, the number saved will be a whole number only.. for eq. 4.2 will be saved as 4.


i suggest u take it as a float array.. put a check whether the number entered is a whole number or not.. in that way, even if the user enters a real number, u can prompt the user to enter a whole number again and proceed with the program only until he enters a whole number...


C++ program?

I have to write a program that will read from a file and store it in an array. whenever i run the program however, it doesn't output the numbers that are in the file. please tell me what i'm doing wrong. Here's my code:





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int main()


{


const int ARRAY_SIZE = 10;


int numbers[ARRAY_SIZE];


int count;


ifstream inputFile;





inputFile.open("numbers.txt");





for (count = 0; count %26lt; ARRAY_SIZE; count++)


inputFile %26gt;%26gt; numbers[count];





inputFile.close();





cout %26lt;%26lt; "The numbers are: ";


for (count = 0; count %26lt; ARRAY_SIZE; count++)


cout %26lt;%26lt; numbers[count] %26lt;%26lt; " ";


cout %26lt;%26lt; endl;


return 0;


}

C++ program?
Is "numbers.txt" in the same directory as your executable? For example in Visual Studio; projectfolder/DEBUG/ or /RELEASE/.
Reply:shouldn't it be numbers[count] %26gt;%26gt;inputfile;


Want to know reason for output of this C++ Code....?

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


class A


{ int a,b;


public:


A(A%26amp; a1)


{


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


}


A()


{


cout%26lt;%26lt;"Constructor A()\n";


}





A(int a=7,int b=8)


{


this-%26gt;a=a;


this-%26gt;b=b;


cout%26lt;%26lt;"this-%26gt;a="%26lt;%26lt;this-%26gt;a%26lt;%26lt;endl;


cout%26lt;%26lt;"this-%26gt;b="%26lt;%26lt;this-%26gt;b%26lt;%26lt;endl;


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


}


void operator ()()


{


cout%26lt;%26lt;"overloaded operator ()\n";


}


};


void main()


{


A a1();


}





...for this A a1();


which constructor is called...?because..nothing is printed on screen or console...?Why?

Want to know reason for output of this C++ Code....?
It usually should have called the constructor A() if you are making the object in maner "A a1;" in the main function. But also you have overloaded the operator "()". SO, it makes a confusion and actually none of the output from the above constructors are called. Another thing, its not a good thing to inital values in parameters. I've often faced these problems when I overloaded the "()" operator. For any further questions you can contact me.
Reply:The compiler does not know which constructor to use for this statement. Will it be A() or A(int,int)? Though it doesn't show any errors if you write it this way, it won't display anything! For making it work do one of the following:


1. when you declared the A(int,int) remove the initial values; this is the reason why the compiler doesn't know if it's this one or the other one.


2. use values when building a1 like A a1(1,1) for example. This way the compiler knows exactly which constructor to call.


3. the first constructor is useless and it's causing you problems; simply delete it!
Reply:#include%26lt;iostream.h%26gt;


class A


{ int a,b;


public:


A(A%26amp; a1)


{


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


}


A()


{


cout%26lt;%26lt;"Constructor A()\n";


}





A(int a=7,int b=8)


{


this-%26gt;a=a;


this-%26gt;b=b;


cout%26lt;%26lt;"this-%26gt;a="%26lt;%26lt;this-%26gt;a%26lt;%26lt;endl;


cout%26lt;%26lt;"this-%26gt;b="%26lt;%26lt;this-%26gt;b%26lt;%26lt;endl;


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


}


void operator()()


{


cout%26lt;%26lt;"overloaded operator ()\n";


}


};


void main()


{


A a1();





void getch();


return 0;


}





The following will not display the output because


the cout are declared inside the class and not in the main()


The reason is due to that a class does not stores any object it creates objects at runing time and destroys them


so it should use constructor and destructor. You should create objects in the main().


C++ question about int main(int argc,char *argv[])?

I am trying to change this main function to …int main(int argc,char *argv[])…


I tried to do it below, can some one help me if it’s right?


Please help me out here!!!


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


Without using int main(int argc,char *argv[])





int main()


{


ifstream in;


ofstream out;


char buf[100];


Steel steel;





cout%26lt;%26lt;"Enter input file name: ";


cin.getline(buf, 100, '\n');


in.open(buf);


cout%26lt;%26lt;"Enter output file name: ";


cin.getline(buf, 100, '\n');


out.open(buf);


if(in)


{


while(!in.eof())


{


try


{


in.getline(buf, 100, '\n');


steel.SetExpression(string(buf));


parser.PrintAssembly(out);


out %26lt;%26lt; endl;


}


catch(IncorrectExpressionException)


{


out %26lt;%26lt; buf %26lt;%26lt;" - Incorrect Expression"%26lt;%26lt;endl%26lt;%26lt;endl;


}


}


in.close();


out.close();


}


else


{


cerr %26lt;%26lt;"Failed to open file"%26lt;%26lt;endl;


}


return 0;


}





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


And changing it to int main(int argc,char *argv[])


Is this right?





int main(int argc,char *argv[])


{


ifstream fin;


// read some data from the file


if (argc == 2)


{


fin.open(argv[1]);


}


else


{


char filename[80];


cout %26lt;%26lt; "Enter input file name: ";


cin %26gt;%26gt; filename;


fin.open(filename);


}


return 0;


}

C++ question about int main(int argc,char *argv[])?
That looks correct assuming you're trying to pass the input file into the program. So you'd run it as:





myprog in.txt





argv would be 2


argv[0] would be "myprog"


argv[1] would be "in.txt"

elephant ear

C++ Using - for -?

My teacher wants me to write the code out using "for"





Question from my teacher:


Enter: 5


1 to 5 = 15 //Example (1+2+3+4+5 = 15)


1 to 5 = 9 //Odd Number (1+3+5 = 9)





I've successful write the code of 1 to 5 = 15


But 1 to 5 = 9 %26lt;%26lt; Odd number I dont know





#include %26lt;iostream%26gt;


using namespace std;





int main()


{


int i=0, max=0;





cout%26lt;%26lt;endl


%26lt;%26lt;"Please enter number 5: ";


cin%26gt;%26gt;max;


cout%26lt;%26lt;endl;





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


sum+=1;





cout%26lt;%26lt;endl


%26lt;%26lt;Total Sum: "%26lt;%26lt;sum


%26lt;%26lt;endl;


return 0;


}





Can someone help me coding out the question that teacher ask me?

C++ Using - for -?
sum = 0;


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


{


sum+=i;


}





the for statement is broken into 3 sections... initialization, comparison, and increment/decrement.





you could have a for loop that looks like





bool done = FALSE;


for (i=0; (i%26lt;10 || done);i=i*2)


{








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


using namespace std;





int main()


{


int i=0, max=0;


int sum=0, myvariable=1;


cout%26lt;%26lt;endl


%26lt;%26lt;"Please enter number 3: ";


cin%26gt;%26gt;max;


cout%26lt;%26lt;endl;





for(i = 1; i %26lt;=max; i++){


sum+=myvariable;


myvariable=myvariable+2;


}


cout%26lt;%26lt;endl


%26lt;%26lt;Total Sum: "%26lt;%26lt;sum


%26lt;%26lt;endl;


return 0;


}





here notice that i have added curly brackets to "for" statement. cos it is more than one line now.





anything you wanna ask about the code, just send your question to my mail, man. efefef20@yahoo.com. i hope this program works for you. it is working as expected in my computer. i think your teacher will like it also. take it easy.


C++ "No Full answers only Hints and links"?

I have to write an application, that reads two time values (times are represented by two int numbers: hours and minutes). Then the program finds out which time is the later time. After that it calculates the time difference between these times. Finally the program displays the smaller (earlier) time and the time difference (duration) in the format: • starting time was 11:22 • duration was 1:04








int main(void) •{ • Time time1, time2, duration; • time1.read("Enter time 1"); • time2.read("Enter time 2"); • if (time1.lessThan(time2)) { • duration = time2.subtract(time1); • cout %26lt;%26lt; "Starting time was "; • time1.display(); • } • else { • duration = time1.subtract(time2); • cout %26lt;%26lt; "Starting time was "; • time2.display(); • } • cout %26lt;%26lt; "Duration was "; • duration.display(); •}














As can be seen from the main function, Time has the following member functions: •read that is used to read time (minutes and hours) from the keyboard. •lessThan that is used to compare two times. •subtract that is used to calculate time difference between two times. •display that is used to display time in the format hh:mm.

C++ "No Full answers only Hints and links"?
For nicer code, and to take advantage of C++ features, you could overload operators for your Time class: operator-, operator%26gt;, operator%26lt;, operator==, ostream operator%26lt;%26lt;, etc. You may not want to get too sidetracked on the niceties, however, because there's some interesting code to be written for your less than and subtraction operations.





Hints:


If the Time class' time attribute is a struct { hour, min }, you can use memcmp for your less than operation. Beware that this only works if you're running on a big-endian architecture. If little-endian, have Time maintain a struct with swapped bytes, and use that for your comparisons.





Code the subtraction operation as if you were doing it by hand on paper. E.g., for 11:22 - 00:30, since 30 %26gt; 22 you need to borrow 1 hour. So you have 10:82 - 00:30 = 10:52.





Decide if you want your Time class to be able to represent negative time. I would suggest a 'sign' attribute. If you have to compute 00:30 - 11:22, you use your less than operator to see that the minuend is less than the subtrahend. Swap minuend and subtrahend, compute 11:22 - 00:30 as before, and set Time's sign to negative.





There's some good stuff to be done here. Have fun!


C++.... Help me fix this code, please?

//Program will find the product of a collection of integers from the user. Terminates when "0" is entered.





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


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


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


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





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


int main(void)


{


clrscr();


textcolor(RED+BLUE);


int i;


int product;


int num;


int bay;


int genki;


int genki2 = 1;





cprintf("Hello. This program will end when you type zero.\r\n");


clrscr();


cout%26lt;%26lt;"Please enter your first integer: ";


cin%26gt;%26gt;num;


clrscr();





if(num !=0)


{


cout%26lt;%26lt;"Please enter an integer: ";


cin%26gt;%26gt;i;


clrscr();


product=i*num;


}





if (product !=0)


{


cout%26lt;%26lt;"Your product is currently "%26lt;%26lt;product%26lt;%26lt;" \n\n";


getch();


clrscr();


}








if(i==0)


{


exit(1);


}





if(num==0)


{


exit(1);


}








do


{


cprintf("Please enter another integer: ");


cin%26gt;%26gt;bay;


clrscr();


bay=bay*product;

C++.... Help me fix this code, please?
You are making the code so complicated. Here is a solution





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





int main() {


int num = 1, product = 1;





printf("Enter an integer (0 to end): ");


scanf("%d", %26amp;num);





while (num != 0) {


product *= num;


printf("The product so far is %d.\n\n", product);


printf("Enter an integer (0 to end): ");


scanf("%d", %26amp;num);


}





return 0;


}
Reply:I am sorry.I dont understand a thing.
Reply:Put an else of if(num!=0) and exit, if you still need help you may contact a C expert at websites like http://askexpert.info/


C++ programming help!?!?

Anyways I can't get the 7th option for my menu to work.


Since, i don't know any other way to do this, i'm gonna post the program in parts....





#include "stdafx.h"


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


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





int bank_menu(void);





const int SIZE=40;





struct


{


int cust_num;


int ssn;


char cust_name[SIZE];


float balance;


int act_type; //1=Checking; 2=Savings


}bank[10];





int main(int argc, char* argv[])





{


int choice;


int cust_index = -1;


do


{


choice = bank_menu();


if (choice == 1)


{


cout%26lt;%26lt;"You have chosen Load Customer into Structure.\n";


if (cust_index == 9)


cout%26lt;%26lt;"The structure is full.\n";


else


{


cust_index++;


bank[cust_index].cust_num = cust_index + 1;





cout%26lt;%26lt;"Enter Social Security Number: ";


cin%26gt;%26gt;bank[cust_index].ssn;


while (bank[cust_index].ssn %26lt; 100000000 || bank[cust_index].ssn %26gt; 999999999)

C++ programming help!?!?
EDIT: I added more at your SHORTENED VERSION link:


http://answers.yahoo.com/question/index?...





----------


I think the problem might be here in the transfer section:





cin%26gt;%26gt;transfer;


int c_index;


int s_index;


int i;


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


{


if (bank[i].ssn == SSN)


{


if (bank[i].act_type == 1)


{


i=c_index;


}


else


{


i=s_index;


....


...





You have a loop for (i=0; i%26lt;cust_index+1; i++)


but you _might_ have not yet assigned a value to cust_index at the time you call the transfer section. Therefore, it's filled with whatever is in memory -- which may be a very, very large number -- and the loop travels on and on, incrementing 'i', until index i is completely off the array bank[] in:





if (bank[i].ssn == SSN)


...





...and the prog hangs/crashes ugly.





That's our 1st and main suspect. You can verify cust_index when calling the transfer section to be sure:





cout %26lt;%26lt; "cust_index is " %26lt;%26lt; cust_index;





...and thus ensure it's a "safe/small' value (and not negative!) before proceeding. Of course, this is just for debugging. You want to remove such stuff when you go final.








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





Loops are fequently the cause of program hang during run time, and it's hard to know if your code has hit some kind of lengthy or even infinite loop without visual indicators.





Optionally, when I'm debugging a tough problem and I need to inspect a loop variable, I might do something like this:





int iii= 20000;





while(iii--)


{


cout %26lt;%26lt; "\r iii is: " %26lt;%26lt; iii ;


///...other code here


}





This code will allow you to inspect the incrementor/decrementor "iii" as it clicks off without cluttering the screen full of numbers. The little "\r" keeps the cursor pinned to the start of the line.





Not only does this give you a strong visual clue, but the cout action allows you to CTR C keybreak out out of the loop easily if you can see the program is stuck in the loop. Much more amiable than calling taskmanager to kill a wayward execution, for example.





You can inspect all of the variables at run time in methods such as this til you pin down the culprit.


---





Use a standard comment by every piece of debug code so you can easily remove it all once fixed:





while(iii--)


{


cout %26lt;%26lt; "\r iii is: " %26lt;%26lt; iii ; //DEBUG ONLY





Then just do a find on string "DEBUG ONLY" for fast clean up.
Reply:You got to be kidding.


Sorry to be mean, but nobody here is going to debug your entire program - which is what you're asking.





Narrow down the problem and ask more specific question.
Reply:Set a break point at the beginning of the 7th choice and single step throught the code, watch the values of the variables. Since the program is crashing there, you are probably trying access memory that isn't valid (e.g. index problem) or something similar.

lady palm

C++ code errors?

I recieve the following errors when I attempt to compile my program, please help.








pim.cpp: In function `void searchContacts(...)':


pim.cpp:112: `count' undeclared (first use this function)


pim.cpp:112: (Each undeclared identifier is reported only once


pim.cpp:112: for each function it appears in.)


pim.cpp:114: `contacts' undeclared (first use this function)


pim.cpp:114: parse error before `||'


pim.cpp:118: warning: implicit declaration of function `int print(...)'


pim.cpp:120: confused by earlier errors, bailing out








void searchContacts(PIM pim.contacts[], int count)


{


int matches = 0;


string name;


cout %26lt;%26lt; "\nEnter first or last name to find: ";


cin %26gt;%26gt; name;


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


{


if (contacts[i].firstname == name) || contacts[i].lastname == name))


{


matches++;


cout %26lt;%26lt; "\nContact " %26lt;%26lt; (i + 1) %26lt;%26lt; ":" %26lt;%26lt; endl;


print(contacts[i]);


}


}


cout %26lt;%26lt; "\nTotal number of matching contacts: " %26lt;%26lt; matches %26lt;%26lt; endl;


}

C++ code errors?
I can help you out, just name the compiler and version (gcc or microsoft c++)
Reply:for (int i = 0; i %26lt; count; i++)





you need to declare count:





const int count=10; or something like that





if (contacts[i].firstname == name





the array contacts is unknown, because your function declaration is broken. use:


void searchContacts (PIM contacts[]...)


or


void searchContacts (PIM *contacts...)





print (contacts[i])





The print function is unknown. You need a function declaration BEFORE searchContacts or in a header file.


C++ help please!! i don't that much about it - i am a beginner!!?

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


//Ch12AppE03.cpp


//display color program


//Created/revised by Armando on 04/28/2008





#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 itemNum = 0;


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 transform(itemNum.begin(), itemNum.end(), itemNum.begin(), toupper);


{

C++ help please!! i don't that much about it - i am a beginner!!?
you have some logic errors in here, that I am going going to touch base on right now. I will let you firgure them out. But I will point out some other things.


1) towards the top, you may want to replace all of the "using std::xxxx;" statements you have with the one statement "using namespace std;" It is simpler to write that one statement than many of the using std:: statements and it solves one of the other problems you had regarding std::transform.





2 string itemNum = 0; will cause an error. Make it string itemNum = "";





3) you have:


else if transform(itemNum.begin(), itemNum.end(), itemNum.begin(), toupper);





is wrong because you need parenthesis after the "if" and at the end of the conditional. Also, you do not put a semi-colon there. You will not get the results you want if you do. Also, if you want to have multiple statements executed if that conditional is true, you need to use curly-brackets to group those blocks of code.





if(conditional)


{


statement;


statement


}


else if(conditional)


{


statement;


statement;


}





4) don't use transform. To get the tranformation you want, you would have to include two more libraries and change your transform to transform(itemNum.begin(), itemNum.end(), itemNum.begin(), (int(*)(int)) toupper)





instead (if the 3rd char is the letter you want) put #include %26lt;cctype%26gt; at the top and use the toupper(char c) function.


so in each if put:


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


cout %26lt;%26lt; "The color is blue" %26lt;%26lt; endl;


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


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





There are still logic problems in your code the way it is now... but if you make changes and get it to compile, then you will see them and be able to correct them.
Reply:you forgot an open parenthesis on the line:





else if transform(itemNum.begin(), itemNum.end(), itemNum.begin(), toupper);





right before transform.
Reply:Thank you for a nice tasty morsel to chew on. Since this is for school, I think a progress report rather than a solution is in order. Your compiler says:





Error1error C2061: syntax error : identifier 'transform





Error 2error C2181: illegal else without matching if





I frankly assume this means you are working on an IDE. If so look at the code window and the line with the first reported error should be illuminated. To add to the hint, since I'm working on GCC from the command line, my first set of errors from the unedited code are:





Ch12App03.cpp: In function ‘int main()’:


Ch12App03.cpp:33: error: expected `(' before ‘transform’


Ch12App03.cpp:38: error: expected primary-expression before ‘else’


Ch12App03.cpp:38: error: expected `;' before ‘else’


Ch12App03.cpp:41: error: expected primary-expression before ‘else’


Ch12App03.cpp:41: error: expected `;' before ‘else’





Okay. What it's looking for is the connection to the if. You and I know it's there. In my document it's about 30. When I go back up and look at it I notice something: for most of the ifs and else lines you have a colon in the end. No.





The lines:





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


and


if (i==16); do nothing. That is because the colon ends the statement. Everything that happens after is outside the scope of those two statements, and whatever you wanted to happen while i was an even number between -1 and 40 will happen once when i==40, while if i==16 the computer will do what you wanted it to -- but if i!=16 the computer will do the same stupid thing because the new commands are outside the scope of the if statement. That's what the illegal else without matching if statement means. DO NOT put a colon at the end of ANY test statement. Put them at the end of statements to be executed in test statements but DO NOT PUT THEM at the end of any test statement.





The syntax error for transform() is gnarlier. In essence you are warned 1. that the parentheses are very problematic, and 2, when I got them sorted out the compiler complained thus:





Ch12App03.cpp:42: error: could not convert ‘std::transform [with _InputIterator = __gnu_cxx::__normal_iterator%26lt;char*, std::basic_string%26lt;char, std::char_traits%26lt;char%26gt;, std::allocator%26lt;char%26gt; %26gt; %26gt;, _OutputIterator = __gnu_cxx::__normal_iterator%26lt;char*, std::basic_string%26lt;char, std::char_traits%26lt;char%26gt;, std::allocator%26lt;char%26gt; %26gt; %26gt;, _UnaryOperation = int (*)(int)throw ()](itemNum.std::basic_string%26lt;_CharT, _Traits, _Alloc%26gt;::begin [with _CharT = char, _Traits = std::char_traits%26lt;char%26gt;, _Alloc = std::allocator%26lt;char%26gt;](), itemNum.std::basic_string%26lt;_CharT, _Traits, _Alloc%26gt;::end [with _CharT = char, _Traits = std::char_traits%26lt;char%26gt;, _Alloc = std::allocator%26lt;char%26gt;](), itemNum.std::basic_string%26lt;_CharT, _Traits, _Alloc%26gt;::begin [with _CharT = char, _Traits = std::char_traits%26lt;char%26gt;, _Alloc = std::allocator%26lt;char%26gt;](), toupper)’ to ‘bool’








In other words, it doesn't like me saying it exists or it doesn't. Good luck.


C++ box application?

the application should run like:


how big: 5 %26lt; user input





* * * * *


*space*


*space*


*space*


* * * * *





Ignore the space











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





int main()


{


int size;





cout %26lt;%26lt; "How Big? ";


cin %26gt;%26gt; size = 444;





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


{


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


{


cout %26lt;%26lt; "* ";


}





cout %26lt;%26lt; "*" %26lt;%26lt; endl;


}





return(0);


}

C++ box application?
I didn't try to run your code, but it looks like it won't work. I see you want to print spaces in between the stars in the top and bottom edges of the box. This is good, since it'll give you a better looking box, but it makes the code a bit more complicated than it would be with a solid top and bottom (which would look like a rectangle on the terminal).





The first thing to realize is that you're only doing two different things: printing the top/bottom, or printing the rest of it. To avoid duplicating code, you could create a function like this:





void horizSide(const int len, const char c, const char spacer) {


for (int i = 0; i %26lt; len-1; i++) {


cout %26lt;%26lt; c %26lt;%26lt; spacer;


}


cout %26lt;%26lt; c %26lt;%26lt; endl;


}





Then your algorithm is: call horizSide to print top, print middle of box, call horizSide to print bottom.





If you define:





const char star('*');


const char spacer(' ');





then the only line you need in the loop between calls to horizSide is:





cout %26lt;%26lt; star %26lt;%26lt; setw(size*2-2) %26lt;%26lt; setfill(spacer) %26lt;%26lt; star %26lt;%26lt; endl;





Note that I defined horizSide as I did so you can use some other spacer character and it'll still look right.





I'll let you code the loop and put it all together. You should be displaying a nice looking box in no time.


C++ project, really hard for me. about Square root?

if anyone answers this question exactly then they get 10 points.





my question is that i have to write a program that returns the square root of a number as a multiplier and a square root. i mean like if someone enters 45 then i should be able to cout%26lt;%26lt;"square root of %26lt;%26lt;num%26lt;%26lt;"times the square root of(i need to put a 5 here but how) "; i already know about the sqrt function.





this is what i got so far.





#include %26lt;math.h%26gt;........other includes are included.


main()


{


int num;





cout%26lt;%26lt;"Enter number to be square rooted: ";


cin%26gt;%26gt;num;





cout%26lt;%26lt;"The number square rooted is "%26lt;%26lt;sqrt(num);


}

C++ project, really hard for me. about Square root?
Yah, you want someone to do your homework for you. How freaking lame can you get.
Reply:cout %26lt;%26lt; "The squareroot of " %26lt;%26lt; num %26lt;%26lt; " is " %26lt;%26lt; sqrt(num) %26lt;%26lt; endl;





cout %26lt;%26lt; "The square of " %26lt;%26lt; sqrt(num) %26lt;%26lt; " is " %26lt;%26lt; num;
Reply:Does not look easy, may be you can contact a C++ expert live at website like http://oktutorial.com/ .

snow of june

Please help with C++ I have two errors?

error C2064: term does not evaluate to a function taking 3 arguments





error C2447: '{' : missing function header (old-style formal list?)





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cmath%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::setprecision;


using std::ios;


using std::setiosflags;








//function prototypes


void getTestScores(double, double, double);


void calcAverage (double, double, double);


void displayAverage (double);





int main()


{





double score1 = 0.0;


double score2 = 0.0;


double score3 = 0.0;


double calcAverage = 0.0;


double average = 0.0;


//get input items


getTestScores(score1, score2, score3);





//calculate average score


calcAverage (score1, score2, score3);


//display output item


displayAverage (average);





return 0;


} //end of main function





//*****program-defined functions*****


void getTestScores(double, double, double);





{


cout %26gt;%26gt;"Testscore:";


getline(cin, score);


cout %26lt;%26lt; "calcAverage: ";


cin %26gt;%26gt; average;


cout %26lt;%26lt; "displayAverage: ";


cin %26gt;%26gt; average;





} //end of getInput function

Please help with C++ I have two errors?
Looks like lots of things are wrong here. But the first error is that you declare a function calcAverage, then when Main roles around, you try to assign calcAverage a value as if it is a variable.
Reply:Ok.





First, I'm sure about the second error... If you check, when you're defining the function getTestScores, after the end of the main function, you are defining it in the wrong way:





void getTestScores(double, double, double);





This works only to declare the function, but not for defining it. You should include the names of the three variables and should NOT include a semicolon before the brace opening, change it to something like:





void getTestScores(double sc1, double sc2, double sc3)


{





etc.





About the first error, I think it happens because you have a local variable with the same name of the function, so, when the compiler finds the line





calcAverage(score1, score2, score3);





it resolves calcAverage as a double and not as a function.





You should change one of the names, the function name or the variable name.





Greetings,





Daniel.
Reply:The first error is probably because you've defined 'calcAverage' to be a double inside of main. Rename either the double or the function and that error should go away.





The second error is because you put a semicolon at the end of getTestScores in the function definition. Remember that error, because you'll see it a lot. It almost always means "oops, I put the semi in the function definition".





void Foo( int x ) //no semi here!


{


//do stuff


}





If you declare a function, then define it later, then you do put a semi.





void Foo(int x);


Plz point out the problem with the following code in C++?

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


#define n 8





void disp(int[]);





void main()


{


int a[n]={1,2,3,4,5,6,7,8};





int i, j, k=1;





i=n/2;





j=i;





k=j-1;





int save=a[n-2];





cout%26lt;%26lt;save%26lt;%26lt;" "%26lt;%26lt;a[n-3]%26lt;%26lt;endl;





disp(a);





cout%26lt;%26lt;endl;





cout%26lt;%26lt;save%26lt;%26lt;"a"%26lt;%26lt;endl;//correct value upto his point.





while(i%26gt;0)





{


cout%26lt;%26lt;"**** save *****"%26lt;%26lt;save %26lt;%26lt;endl;


a[i+i]=a[i];





i--;





}





cout%26lt;%26lt;save%26lt;%26lt;"b"%26lt;%26lt;endl;//value changes here. why?





while(j%26lt;n)


{


a[j-k]=a[j];





j++;





k--;





}





cout%26lt;%26lt;save%26lt;%26lt;" "%26lt;%26lt;a[n-3]%26lt;%26lt;endl;





a[n-3]=save;





disp(a);





}





void disp(int a[])


{


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


{


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





}





}

Plz point out the problem with the following code in C++?
I have just compiled and executed your program. The value of "save" does not change. So what's your problem exactly?
Reply:the problem that is causing problem is value of n


the alternative to use it is


redefine the disp(a[])function by disp(int a[],int size_of_a)


which will be the size of array a


but it is of quite consideration the value of save is not changing if you could explain what the purpose of program is i may provide better answer
Reply:You will never get what you wants due to bad indentation. Good luck!


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.


Translating loops in C++?

How would I translate this loop into a while loop





int power2 = 1;


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


power2 *= power2;


}


cout %26lt;%26lt; power2;





Translate this switch statement into equivalent if or if / else statements.


switch (xx) {


case 1: cout %26lt;%26lt; "Hello";


break;


case 3: cout %26lt;%26lt; "Goodbye";


break;


default: cout %26lt;%26lt; "Never mind.";


}


If you don't know please don't answer; Thanks!

Translating loops in C++?
int i=0;


while (i %26lt;n)


{


power2 *=power2;


i++;


}


cout %26lt;%26lt; power2;





if (xx == 1)


cout %26lt;%26lt; "hello";


else if(xx ==3)


cout %26lt;%26lt; "Goodbye";


else


cout %26lt;%26lt; "Never mind";
Reply:you get help from http://tutorialofc.blogspot.com/

sweet pea

Help with a c++ program that converts uppercase to lowercase and vice versa?

I've been working on this program and I can get it to work, but it only works with a single character. I want to be able to enter a few letters in and have them all converted to the opposite case. I'm new to arrays so if someone could please help me I'd appreciate it. This is what I have so far:





#include %26lt;cctype%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main()


{


char ch;





cout%26lt;%26lt;"Enter a few letters: ";


cin%26gt;%26gt; ch;





if ( isalpha ( ch) ) {


if ( isupper ( ch) ) {


ch = tolower ( ch );





cout%26lt;%26lt;"The lower case equivalent is "%26lt;%26lt; ch %26lt;%26lt;endl;


}


else {


ch = toupper ( ch);





cout%26lt;%26lt;"The upper case equivalent is "%26lt;%26lt; ch %26lt;%26lt;endl;


}


}


else


cout%26lt;%26lt;"The character is not a letter"%26lt;%26lt;endl;





cin.get();


return 0;


}

Help with a c++ program that converts uppercase to lowercase and vice versa?
int i;





while (string[i]) {


string[i++] = toupper( string[i] );


}
Reply:You have no arrays in this at all. ch is declared as a char, not as an array of char, which is what you would want. try doing a global replace of chi[i] for ch, and then replacing it i with -- say 20 in the declaration section. Then put everything in a for loop or a while loop (while (ch[i]!='\0'"){ and make sure you initialize i to 0 and do i++ in your program.)


C++ square root calculator?

this is my code but it doesnt want to work right lol





its very basic


but if someone could help me i would appreciate it





#include %26lt;iostream%26gt;





using namespace std;





int main(void)


{


double dfirst;


double dsecond;


double dnum1;


double dnum2;


int answer;











{


cout %26lt;%26lt;"enter your first number" %26lt;%26lt; endl;


cin %26gt;%26gt; dfirst;


}





{


cout %26lt;%26lt; "enter your second number which needs to be less than the first"%26lt;%26lt; endl;


cin %26gt;%26gt; dsecond;


}


(dfirst / dsecond == dnum1);


(dsecond + dnum1 == dnum2);


(dnum2 / 2 == answer);





{


cout %26lt;%26lt; answer %26lt;%26lt; endl;





}





system("pause");


return 0;





}

C++ square root calculator?
ok , there is alil problem u ddnt notice may b , that :


in C , the symbol == is called the equlity operator. Recall that the symbol = is called the assignment operator.Remember that the eulity operator, == , determines where two expressoins are equal , that gives u a result of true or fales, only , but the assignment operator ,=, assigns the value of an expression to a variable. And all u need here is the assigmnemt operator not the equality one, so ur code is gonna be like:


# include %26lt;iostream%26gt;


using namespace std;


int main(void)


{


double dfirst;


double dsecond;


double dnum1;


double dnum2;


int answer;


{





cout%26lt;%26lt;"enter your first number"%26lt;%26lt;endl;


cin%26gt;%26gt;dfirst;


}


{


cout%26lt;%26lt;"enter our second number which needs to be less than the first"%26lt;%26lt;endl;


cin%26gt;%26gt;dsecond;


}





(dnum1=dfirst/dsecond);//16


(dnum2=dsecond+dnum1);//3


(answer=dnum2/2);





{





cout%26lt;%26lt;answer%26lt;%26lt;endl;


}





system ("pause");


return 0;


}





and ur square root calculater will work , enshallah.

bottle palm

C++ Strings?

I have to take in a a string and scramble it 5 times, using string methods. I have to separate it using 2 radnom num's, and into three parts. It won't execute for some reason. Any help will be appreciated. Here's the code:


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter a phrase to shuffle: ";


getline(cin,phrase_input);





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


{


beg = phrase_new.substr(0,random_one);


mid = phrase_new.substr(random_one,random_two - random_one);


endphr = phrase_new.substr(random_two,phr_len - random_two);





shuffled_phrased+= endphr;


shuffled_phrased+= beg;


shuffled_phrased+= mid;





}


cout%26lt;%26lt;shuffled_phrased;

C++ Strings?
first, I assume that somewhere you are copying phrase_input into phrase_new somewhere and just didnt show the code.





second, make sure your random_one and random_two are not greater than the length of the string phrase_new.





third, to loop 5 times, your "for" loop should be "for(int i = 0; i %26lt; 5; i++)"





fourth, you need to copy shuffled_phrased back into phrase_new at the end of your loop, so you can repeat the operations on it again.
Reply:Do you know how to use a debugger?


If not, I suggest you learn.





A debugger will help you to pinpoint a problem to a specific line of code. Learn to step through the code and verify it is doing what it is supposed to be doing.





Learn to use breakpoints and the call stack.


C++ trouble?

I am having trouble to simplify the answer.


using namespace std;


int main(){


double dblA, dblB, dblC, dblD, dblproductN, dblproductD,dblsumN,dblsumD, dblGCF, dblsimplifyNsum, dblsimplifyDsum;


double dblsimplifyNproduct, dblsimplifySproduct;


int M,N;


int R=M%N;


while(R!=0)


{M=N;


N=R;


R=M%N;}





{


cout%26lt;%26lt;"Enter the numerator and the denomenator of the first positive fraction separated by a space=%26gt;"%26lt;%26lt;endl;


cin%26gt;%26gt;dblA%26gt;%26gt;dblB;


cout%26lt;%26lt;"Enter the numerator and the denaminator of the second positive fraction separated by a space=%26gt;"%26lt;%26lt;endl;


cin%26gt;%26gt;dblC%26gt;%26gt;dblD;


dblproductN =(dblA*dblC);


dblproductD =(dblB*dblD);


dblsumN = (dblA*dblD)+(dblC*dblB);


//dblGCF = N;


//dblsimplifyNsum= (dblsumN)/(dblGCF);


//dblsimplifyDsum= (dblproductD)/(dblGCF);


//dblsimplifyNproduct= (dblproductN) / (dblGCF);


//dblsimplifySproduct = (dblproductD) / (dblGCF);


cout%26lt;%26lt;"the sum of the fractions is" %26lt;%26lt;dblsu

C++ trouble?
http://en.wikipedia.org/wiki/Euclidean_a...


http://en.wikipedia.org/wiki/Binary_GCD_...