cout Not Working Reliably - c++

I was working on a school project to make a blackjack/21 console application. The program is somewhat long and complicated, so will only put the offending code here. Complete code will be below.
cout << "Before comp instruct";
cout << endl << "Computer: ";
cout << "After comp instruct";
It will always print the first statement, but it sometimes stops immediately after that. It happens only sometimes, depending on the input in the program. I (and my teacher and peers) could not figure out why this program stops after the first statement. At least one other person in the class is having a similar problem.
These were done using CodeWarrior IDE on a Windows 7 Dell desktop.

Related

An automatic grader in c++

I am trying to write an automatic grading program for a C++ course I am teaching. Usually, automatic graders use (input,output) pairs: the submitted program reads from standard input and writes to standart output, and the grader compares it with the expected output. But I want my students to practice specific C++ constructs (and not e.g. write a program in C), so the tests are written in C++. In a simple example, I give them a main program such as:
#include "func.hpp"
...
int main() {
test(func(1)==2);
test(func(2)==33);
...
/* some 100 tests, including some randomized tests */
...
cout << "Grade: " << grade << endl;
}
The students have to submit the files func.hpp and func.cpp (for more complex assignments, some more files are required).
There is a bash script that compiles main.cpp with func.cpp, runs the program and reads the grade from the last line (It is run within a docker container, to prevent inadvertent damage to the host computer).
The problem is that a student can, from within func, print "Grade: 100" and exit.
Is there a simple way to make the grader more reliable?
I don't want to go crazy with this, but if you want to make it safe, you should put the student work in a "clean room" so to speak. This is a tedious exercise, but once done, it will be very safe. You said you run in a docker container, so I would run the student's work in a sub-process with IO redirected (to a pipe, file, or to /dev/null, depending on whether some of your assignments involve console output). That way a smart-ass student can print whatever they want, but they will only be piping it to you, not printing to the console. I'm not going to go into the code for this - I saw lots of examples I liked here on Stack Overflow by searching for "fork with redirected stdout" and it's a classic forumula you probably know.
In pseudo code, it would look something like:
main() {
<tedious setup for stdin/stdout/stderr redirection>;
int ch = fork();
if (ch == 0) {
test(func(1)==2);
test(func(2)==33);
.
.
// test clearly generates "grade" so use it as an exit code as an
// easy way to return the information you want
exit(grade);
} else {
for (;;) {
<wait on and read stdout and stderr and do whatever you want to it - it won't go to the console, so no dirty tricks.>;
<you could analyze the student's output as part of grading, or pass it to the console with a highlight, so you know it's not your grade output>
<wait on ch to exit, get the exit code, display it as the grade and break>;
<generate some input if you want to have an assignment where the students read from stdin>;
<add a time-out on waiting for the child to exit. That way evil student can't just hang or busy-loop, whether on purpose by accident.>;
<if it seems to be hanging, you have to process id and can kill it.>;
}
}
}
Obviously the above is sketchy. When you loop on managing a child process, you have to check on everything simultaneously with select or something similar, but you sound like a pretty senior engineer. You get the basic idea. The students can't get away with anything if you keep their run-time environment in a child process that you manage. Plus it gives you you a platform to pull the strings of their code anyway you see fit.
Easy. When you do the analysis, just use a different main than what you gave the students as a reference program. Your version prints your secret key.
int main() {
test(func(1)==2);
test(func(2)==33);
...
/* some 100 tests, including some randomized tests */
...
cout << "The student doesn't know my version prints this next line" << endl;
cout << "Secret validation code: NCC-1701" << endl;
cout << "Grade: " << grade << endl;
}
A clever student who does what you suggests (prints 100 and exits), won't know about the secret message that your version prints and script validates for.
True story. When I was teaching a networking class, there was a homework assignment to implement a "stop and wait" protocol with UDP sockets. My code implemented the "client". The student's version implemented the "server". One student didn't have the networking calls working, but was clever enough to just print the expected output with sleep calls in between print statements. He would have gotten away with it if I hadn't of stopped my client program first and noticed his was still printing our lines of "incoming" data being received.

Infinite loop after cin c++

I'm pretty newbie at c++, so this confuses me.
This code seems to loop infinitely after std::cin :
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "My name is Martin. What's yours?" << endl;
string name;
cin >> name; //It seems to loop around here
cout << "Ah, so you are" << name << "." << endl; //It doesn't print this message
return 0;
}
Thanks!
EDIT: I'm not sure I understand your answers, but what happens is that when you enter a name and press enter, it does nothing. The terminal just continues to the next line, letting you write something on that one too. It does this infinitely, a little bit like when you just press enter without any text.
EDIT 2: My bad, I should try to clear up things. This is what comes in the JDoodle console:
My name is Martin. What's yours?
JDoodle - Timeout - Some common reasons for Timeout
Your Program may have a endless loop
Please check the program and try again or contact JDoodle support at jdoodle#nutpan.com for more info.
The reason I believe it is a loop is that in a console that doesn't write timeout, I can continue to input on a new line all the time.
Since you are using JDoodle the console will be waiting on the server. If you don't type anything there it hangs until it eventually gets killed for being too slow.
The way to fix this is to enter the text you wish to type before running the program into the box labeled "Stdin Inputs...".
Alternatively you can set the "Interactive mode" to "On" which will show the console in the website where you can enter some text and press enter. (I tried, it works).

C++: Why does the same code run in Eclipse Neon produces different output in Visual Studio 2013?

I've been writing programs for my C++ class this semester and I have run into the problem a few times where I will write some code in Eclipse on my Mac and it does not give me the results I expect, or won't work at all. However, when I run the same exact code in class on a Windows machine with Visual Studio 2013 it works exactly as expected. An example of this is our last lab. One of the things that we had to do required us to use std::cout << '\b';, but this would not happen when I run the program on my Mac.
'\b' will not work on my Mac:
#include <iostream>
#include <iomanip>
using namespace std;
int main(void)
{
//Prints out header to console
cout << left << setw(20) << "Binary Number" << right << setw(20) << "Decimal Equivalent" << '\b' << endl;
return 0;
}
The code gives the same output without the backspace:
Does anyone know what could be causing this?
Edit:
I edited this post to be more general with my example instead of specific to my project. This problem also came up for one of my classmates who was doing his C++ coding in XCode.
The Eclipse console does not support handling backspace.
There is a long standing bug report for this (Eclipse bug 76936) At one point there was a fix but it was reverted as it caused problems. So the current state is that it still that it doesn't work.

Xcode debugger not showing C++ cout output

I am currently trying to get the hang of the debugger in Xcode while I am trying to learn C++. I'm coming across a problem where the debugger is not showing any cout output to the console. It just says (lldb)
I've set breakpoints to make sure I don't miss anything:
4 variable outputs
As you can see this piece of code has already been run:
int x = 1;
std::cout << x << " ";
However, the console is still only showing me the following:
console output
I can step over each statement and it still won't show anyting but (lldb)
To my knowlegde the debugger should be showing 1 2 4 8 sequentially as I step over/step into each statement. However, the results are only output the console after I finish debugging.
My question essentially, why am I not seeing anything being displayed?
For the record this is my first question, if I've not searched well enough or broken any rules please feel free to let me know, thanks in advance.
You need to terminate your output line, e.g.:
std::cout << x << "\n";
or
std::cout << x << std::endl;
Your output should show up after your have written a line terminator character as in either of the statements above.

C++ boolean logic syntax for xcode on Mac

I just learned how to program in C++ this last week, and I wrote my first program using Microsoft Visual Studios 2010 express. My computer at home is a Mac and doesn't have MS Visual studios so I decided to use xcode and copy my code from MS visual studios into xcode. For the most part there aren't any errors except that xcode has a problem with my boolean logic syntax. Here are a few of the examples from my code that it has a problem with:
if(place == 1)
{
cout<< name << " 'IS NOW THE FASTEST JUNIOR IN THE UNITED STATES!!!\n";
cout<< "ABSOLUTLY INCREDIBLE!!!!! WHAT AN AMAZING RACE\n"
<< endl
<< endl;
cout<< "You did it! You won the race and are now the fastest Junior skier in the USA!\n";
cout<< "All that hard work really paid off for you!\n";
<<endl;
}
else (place >1)
{
cout << "You skied a great race, but unfortunately you did not beat Ben.\n";
cout<< "You can always race again next year and shoot for gold.\n"
<<endl;
}
The error that comes up says "Expression result unused." How do I fix this?
If we start with the first line of your code snippet (which was much easier to read and understand after formatting it properly):
else(place >1);
This is the last part of an if statement, and means that if the condition in the if was not true, then do the statement after else. In your case it's an expression that checks if place is larger than one then throw away the result, so it does nothing really. You probably mean to use else if instead of only else.
Then look at the next line:
else (restStopDecision == 2);
Here you do the same mistake again, but now with an else where none are supposed to be, which is an error in itself. You probably mean else if here too.