Programming :: C++ Cin Doesn't Wait For Input By Operator
Jan 4, 2010
I am trying to learn C++.I implemented a simple archive program, and I am in a situation in which the user is prompted by a menu to make a choice.So I have some cout instruction to illustrate the possible choices and then
int choice;
cin>>choice;
and everything works fine.I introduced this code in a "while" loop that checks wether the choice made by the user is valid or not:
bool check=true;
int choice;
while(check)
{
cin>>choice;
if(the choice is valid) {...;check=false}
else cout<<"please make an other choice"
}
What is happening is that if by mistake the user introduces a character in place of a number, the loop repeats indefinitely because the program, when it get to the "cin" instruction, does not pauses to wait for a new input.
I'm having a bit of an issue using overloaded operators in an already overloaded operator. In my following code, I have overloaded the && operator to compare two Course objects. The operator in turn goes to a function which calls other overloaded operators to compare private object variables of that object to compare them.
I'm building a simple(?) socket server using threads to serve up a few requests. The spec is such that I have to listen to three ports at once, so I decided to use pthread to create three separate threads that would wait for connections, then spawn new threads to handle them.
The problem is that when I do this, for some reason the program never enters the wait loop and instead terminates (All three threads did get created since the messages get printed properly.) It gets to the line which prints "???", but not the line after the accept() call.I don't see an open port when I check for one either so I'm 99% sure they're terminating.Basically I have a main() method which has three calls to pthread_create, which should result in three threads being run that all wait for connections (listenOnPort). After each thread creation I print some info to make sure it's actually being created.
By the way, when I just run listenOnPortwithout threading, the server appears to enter the loop correctly and seems to be waiting for requests. It's only when I run the functions as threads that the problem seems to happen.The source is attached below. Any help will be appreciated. Much of the code is borrowed from a website (I can't post it because I am new here.) You need not worry about the handler_ methods because those are just methods that are run by the threads themselves.
Also--the original source was in C and I changed it to C++. Should I just use C? server.h Code: /* * server.h
The goal of the script is the following:Whenever I leave my house, my wife comes to my computer to check her mails (She has two computers of her own but they have a dangerous operating system, so they have been banned from the internet).She shuts down qbittorrent so as to download all her mails, and she forgets to execute it again on 98% of the times. End result: my computer is sometimes turned on for 10 hours doing absolutely nothing.I had therefore two choices: a) get on my wife's nerves until she gets the problem or b) write a bash script.If you are married, you will understand that bash scripts are easier to deal with than spouses.Ergo, I wrote the following script:
I have a binary value which I receive from a controller. Say this binary value is 42. Just plain hex 42. If you would look at that byte in a debugger you would see 42.Now this value hold 8 bits each indicating a high or a low output. So 0x42 = 01000010b. Which means bits 1 and 6 are '1'.When I would want to find out which bits are set and which are not in a language like C, I simply do:
Code:
mask = 0x80; if (binval & mask) {...} etc..
However I am programming in TCL, and I try to do:
Code:
set mask 0x80 if { [expr ($binval & $mask) > 0] } {...} etc...
this fails. At the moment the expr is executed, $binval is evaluated and substituted so the expression I am evaluating is
Code:
set mask 0x80 if { [expr ('B' & '0x80') > 0] } {...} etc...
Eventually I got it working by converting the $binval into a '0x42' string value, like this:
Code:
binary scan $binval c byte set byte [format "0x%0x" $byte] set mask 0x80 if { [expr ($byte & $mask) > 0] } {...} etc...[
Then the expression yields what I want. But this seems so stupid and clumsy. Isn't there a better way where I can compare two binary values without conversions?
I have a server listening on incoming client connections. Once the client establishes SSL connection with the server, the server waits on read() from the client. Only Client can disconnect the connection. I want to have a timer in the server program to wait for x secs after read() and then disconnect the Client connection.
I have a simple program from book C++ cookbook, page 291, 8.3, Using Constructors and Destructors to manage resources (or RAII), but it can not get compiled in my g++
------------------------------------------------------------------------------------------------ // Example 8-3. Using constructors and destructors #include <iostream> #include <string> using namespace std;
I'm very new at Bash scripting and have a bone head issue that I'm trying (and failing) to resolve. I cannot get this one IF statement to work, it seems the comparison operator does not think the resulting number from the $b*$c+$b operation is an integer even though it is a number. Below is a small proof of concept script with the bit I'm having trouble with.
Is there a way to achieve the following with the square bracket operator?I have a class:
Code: Select allclass A { public: void SetValue(int index, Item* B); private: int m_iCount; ItemCollection Item_Collection;
[code]....
The probelm is that I am not just blindly assigning whatever given on the right hand side to the returned value from operator [], I need to verify it's not NULL, and it is not already there in the Item_Collection, and I also need to increase the count.
I know it is wrong to use the "==" operator to compare the equality of two floating point numbers. Logically it would seem that if the "==" operator is not usable for floating point comparison, then the "<=" operator would also not be usable. Is this true? The lack of google search results on this topic made me think that it must be true. If that is true, then is it true that the only way to compare floats with <= or >= is with something like the code below? Code: bool smaller_than_or_equal(float a, float b) {
if ( fabs(a-b) < EPSILON){return true;} else if (a < b) {return true;} else {return false;}
} I think this is a general question, but if specifics are necessary, I am using the C++ language to code at the moment.
Can one of you point me towards a comprehensive print function tutorial in perl? I was under the impression that everything within quotes will get interpolated - but I am running into exceptions where it is not desirable to run some varaibles under quotes.
I've been reading and googling, etc. I've seen some things, but not a definite explanation of this. What is the appropriate way to mimic a C ternary operator using Python? Isn't there an exact proper way to do it? Any difference for using lambda functions? I'm sorry, but I've been searching and it's ambiguous to me how this should be handled. I would appreciate a person's help on this. I came up with this link, but I'm wondering if I'm missing something.[URL]...
So, is that it? In Dive Into Python, it's using the and-or trick. Well, if the above post is the full explanation,
which definitly is not the same number. I guess somewhere in the convertion from double to char* ("<<") something is not right and what can i do to save these double numbers in an accurate manner in a file?
does the assignment operator copy all the members also for structures containing STL objects strings,vectors,vectors of vectors...)? I did try it on my platform,and it works that way (copying all the fields),but i was wondering if it is a standard behavior or not.
I'm writing a script that needs to spawn 2 or more processes and wait for their return status. I have a method to do this by waiting on each process individually like this:
I wanted to know how can I set a period of time to a tcp connection to wait for request or respond for tcp block read. which system call or function I can use? Does any body know a very simple quick and easy reference on web for socket programing that has lots of socket programing examples in it?
My compiler won't find <new>. Since it's such a basic function, I'm a bit confused. Is <new> supposed to be in a file of its own? Where do I find it? And if it turns out to be missing on my computer, where can I find a <new> one? Or do you see anything else that I do wrong?
After some trying out, it seems I can't find stuff like iostream or any other basic stuff either. The compiler is prolly looking for them at the wrong place. Does anyone know how I change or add paths for the compiler to look in when using the "<>" operator?
I have a doubt about signals in C programming. I have done this little program to explain it. It creates a child process with fork and, when the child ends, receives the SIGCHLD signal and wait for its termination.Ok, quite easy, BUT when I execute this code the SIGCHLD signal is received twice, first as an error (returns -1) and the second one to finish the child process.I don't understand the meaning of the first received signal. Why is it generated? Is the code wrong? (if you add the SIGINT and press Ctrl+C during the execution it also receives two signals instead of one)
I recently started shell programming and my task now is to do a menu display.Currently i am stuck whereby user will input both title and author and it will delete it.
I have an Ubuntu 10.04 installation that I have been using for about a month. However, just recently, at random times for about 5 seconds I can't use my mouse or keyboard.When it does that, the computer isn't frozen, I just can't type.About 5 seconds later, all the text that I was trying to type suddenly appears. I have 4GB or RAM, and even when I barely have any programs running and at 0% of resource usage, the problem still persists. This has just been happening for the past day or so.
For some reason on any application that uses sound, but doesn't allow me to specify the input/output audio devices I don't get anything. I want all audio to go to my USB Headset, and not the onboard soundcard. To start off with, here's alsamixer: Here's a screenshot from Mumble (voice chat) that shows that I've set the USB headset device in there, and it works 100% fine on Mumble.
[Code]...
As you can see from the alsa-base.conf above I've tried specifying snd_usb_audio as the default device, but it's not working.
I working on a project, where a central unit (we call it System Controller ) should talk to several peripheral units on different input ports. The system controller have 4 RS485 ports, 1 RS232, 1 USB A, 1 USB B and a switch with 2 TCP/IP ports. The problem is that when the peripheral unit that is supposed to be connected to one of the two TCP/IP inputs isn't connected (which the system should be able to handle), the software thinks that data from the unit connected to the RS232 port is from the unit that should be connected to the TCP/IP.
The input peripherals are defined as: #define CR_DEVICE "/dev/ttyS1" // ttyS1 #define SL_PORT "/dev/ttyUSB0" #define BO_PORT ""
It is the last one which are supposed to receive from the TCP/IP port, and of course the first one which should receive data from the RS232 port.
I've tried installing both ibus-anthy and ibus-mozc in Jessie but I still cannot input Japanese.
By contrast, In Wheezy, when using anthy for example, I can see ibus preferences icon where I can configure ibus and anthy. This doesn't show up in Jessie.