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