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.