Good day,
I have a loop that computes values and displays the output to the console. However, when I run the code, cout << does not display the output I expected. In fact, it doesn't display any output. The only way I know the code even works is that the values are changing.
cout << was working just fine until I used the switch statement. Every piece of text that I expected to print to the console inside the statement and the loop was just dead text. It didn't work. I also tried flushing the stream using cout.flush. That did not work either I cannot include all of my code, but here are the problem lines:
if (exampleloop == '1'){
ExampleValue = (ExampleValue - 1);
ExampleValue = (ExampleValue - ExampleValue);
cout << "ExampleText " << ExampleValue << " ExampleText";
if (ExampleValue <= 0){
cout << "ExampleText.";
}
if (ExampleValue <= 0){
cout << ExampleText";
}
example_label:
ExampleValue = (ExampleValue1 - ExampleValue1);
cout << "ExampleText! " << ExampleValue! << " ExampleText!";
goto Example_label;
}
And the only way I know that the code is being computed
cout << "EXAMPLE TEXT ExampleValue1 at "<< ExampleValue <<" . ExampleText";
In this case, "Example Text" would never appear on the console. However, the "Example Values" would be computed and the changes would reflect on the code above.
My IDE is repl.it
Is this my fault, or my IDE's? And what is going on?
(Yes, I #include iostream and I'm using namespace std)
Related
Basically, whenever I run this program, the output window downsizes itself and I can only see the very end of it. Everything else is not shown even if I resize the console myself, which means that while I can work on it, I can't see most results.
This is how it looks when it first opens
How it looks when I resize it
The code uses a lot of headers, so I'm leaving only the main in order to give you an idea of what I'm working with. Most things are in spanish as I'm natively a spanish speaker (and am required to code in spanish).
It's just a simple menu that allows you to input a specific amount of customers attending a veterinary.
I have no idea what the issue is, and because I'm working with people who are more knowledgeable in coding than me, I can't exactly debug it myself either.
I'm running on CodeBlocks 17.12, on Windows 10.
int main(){
int opcion;
bool salir = false;
do{
system("cls");
cout << "\tBienvenido. Elija la opcion.\n\n";
cout << "\t1)Registrar socio.\n";
cout << "\t2)Agregar mascota.\n";
cout << "\t3)Ingresar consulta.\n";
cout << "\t4)Ver Socios ingresados.\n\n";
cout << "\t0)Salir\n\n";
cout << "\tOpcion: ";
fflush( stdin );
scanf("%d",&opcion);
if (opcion==1){
menuRegistrarSocio();
}
else if (opcion==2){
cout << "Ingrese los datos de la mascota\n";
system("PAUSE");
}
else if (opcion==3){
cout << "Ingrese los datos de la consulta\n";
system("PAUSE");
}
else if (opcion==4)
{
if(sistema.SOCIOS.size() == 0)
{
cout << "No hay socios creados" << endl;
}
else
{
int cont = 1;
map<char*, Socio*>::iterator i = sistema.SOCIOS.begin();
while(i != sistema.SOCIOS.end())
{
cout << cont << "- Nombre: " << i->second->getNombre() << " CI: " << i->second->getCi() << endl;
cont = cont + 1;
i++;
}
}
system("PAUSE");
}
else if (opcion==0){
salir = true;
}
else
cout << " - Comando Incorrecto\n";
}while (!salir);
cout << "\n\n - BYE!\n";
}
All I need is for the output to show correctly, so I can keep coding.
EDIT:
I "fixed" it by quoting the "system("cls");" line. There's a few other system lines in the code, so I quoted them as well, because they were causing the same effect.
I don't really know what they do (never used this before), but now the program isn't completely unusable. The window will still downsize itself, but it shows everything this time, so I can resize it and use it as normal.
I would try right clicking on the top bar of the console window where it says "D:####..." Then clicking Properties > Layout and changing the width and height.
I'm using this code to output nodes of a huffman tree to a text file with a certain formatting. All the node outputs within the if block run as expected, but the first output in the else block is missing the '0' fill character after the "L:". It should output "L:076" but instead is outputting "L: 76". The cout looks correct but the text file isn't. All future loops through the else block output like they should, it's only the first loop that is missing the fill character. Here's a picture of my output
void preOrder(node* tree, std::ofstream& of) {
if (tree->label > 0) {
of << "I:" << tree->label << " ";
}
else {
std::cout.width(3);
std::cout << std::right;
std::cout.fill('0');
std::cout << int(tree->ch) << std::endl;
of << "L:";
of << of.fill('0');
of << std::right;
of << int(tree->ch);
of << " ";
return;
}
preOrder(tree->left, of);
preOrder(tree->right, of);
}
From cppreference.com:
The second form (2) sets fillch as the new fill character and returns the fill character used before the call.
"The second form" is the non-const version, that applies here. So my guess (I never used fill myself and I cannot compile your code as it is) would be that the call is correctly applied and then you put the old fill character (blank space presumably) to the stream, because you do:
of << of.fill('0');
Also, I noticed that you dont set the width of of.
Because you're hiding something naughty from us.
#include <iostream>
int main()
{
std::cout.width(3);
std::cout << std::right;
std::cout.fill('0');
std::cout << 3 << std::endl;
return 0;
}
Outputs 003 (live example).
Please provide an MCVE and I'll edit my answer to help you.
I am writing code for a project in my computer science course and I am testing my algorithm to see if it works and how fast it is. I know that the algorithm works because when I launch the program in Visual Studio 2013, I get the correct output. But, when I launch the .exe from the Visual Studio projects folder in the command line or from windows explorer, the first two cout statements are displayed correctly, but the cout statements during the for loop are not displayed at all. Again this only happens when I launch the .exe outside of Visual Studio. It isn't a big problem, but I wonder what's going on here. Thanks.
Here is int main() (The first 2 couts work and the others don't):
int main() {
// declare input stream for reading dctnryWords.txt
ifstream inFile;
// create a pointer to memory in the heap
// where each word in the dictionary will be stored
string* words = new string[DICTIONARY_SIZE];
// create a vector of forward_lists to hold
// adjacent words for each word in the dictionary
vector< list<string> > adjacents(DICTIONARY_SIZE);
// open dctnryWords.txt
inFile.open("dctnryWords.txt");
// load words into RAM
cout << "Loading words into RAM took: "
<< time_call([&] { copyDictionary(inFile, words); })
<< "ms\n";
cout << "Finding adjacent words took: "
<< time_call([&] { searchAdjacents(words, adjacents); })
<< "ms\n";
for (int i = 0; i < DICTIONARY_SIZE; i++) {
if (adjacents[i].size() >= 25) {
cout << words[i] << "(" << adjacents[i].size()
<< "): ";
for (list<string>::const_iterator j = adjacents[i].cbegin(); j != adjacents[i].cend(); j++) {
cout << *j << " ";
}
cout << endl << endl;
}
}
return 0;
}
I'll bet a nickel your program isn't finding "dctnryWords.txt" when you launch it elsewhere... because it's going to look in the current directory, which is likely different when you run it outside of VS.
I'm very new to programming in C++ but I'm trying to write some code which filters a specific word from a string and then takes a specific action. For some reason the code does not see the text inside the string.
printf("%s \n", data.c_str());
cout << data;
This shows absolutely nothing - meaning I cannot use .find or write it to a file.
printf("%s \n", data);
This shows exactly what I need.
I am writing the code into data with assembly:
mov data, EDX
Why is that I can only use the the last method?
Edit:
Data is initiated as:
std::string data;
If a pointer to a string is null all subsequent calls to cout
stop working
const char* s=0;
cout << "shown on cout\n";
cout << "not shown" << s << "not shown either\n";
cout << "by bye cout, not shown\n";
The two function calls are not equivalent, as \n at printf flushes the stream. Try with:
cout << data << endl;
Be sure you used
#include <string>
in your file header. With this in place you should be able to use
std::cout << data << endl;
with no issues. If you're using a global namespace for std you may not need the std::, but I'd put it anyway to help you debug a it faster and eliminate that as a possible problem.
In Short
You will have a problem with cout, if you don't put a linebreak at it's end!
In Detail
Try adding an endl to your cout (e.g. std::cout << data << std::endl), or use following instruction to activate "immediate output" for cout (without it needing a linebreak first).
std::cout << std::unitbuf;
Complete example:
std::cout << std::unitbuf;
std::cout << data;
// ... a lot of code later ...
std::cout << "it still works";
Sidenote: this has to do with output buffering, as the name unitbuf suggests (if you want to look up what is really happening here).
This way it is also possible to rewrite the current line, which is a good example, where you would need this ;-)
Practical example
using namespace std;
cout << "I'm about to calculate some great stuff!" << endl;
cout << unitbuf;
for (int x=0; x<=100; x++)
{
cout << "\r" << x << " percent finished!";
// Calculate great stuff here
// ...
sleep(100); // or just pretend, and rest a little ;-)
}
cout << endl << "Finished calculating awesome stuff!" << endl;
Remarks:
\r (carriage return) puts the curser to the first position of the line (without linebreaking)
If you write shorter text in a previously written line, make sure you overwrite it with space chars at the end
Output somewhere in the process:
I'm about to calculate some great stuff!
45 percent finished!
.. and some time later:
I'm about to calculate some great stuff!
100 percent finished!
Finished calculating awesome stuff!
There is some problem during the run time of my program and i am unable to get what the problem is.
what happens basically is , my program automatically closes and displays the following in Microsoft visual c++ 2010 express window
What could be the reasons for this ? I have no idea why this is happening.
Let me tell that in my program i have used pointers too often and have used character arrays which i write to the disc
The program is too large to display
This is the function called after which my program stops :
void display_databases()
{
struct info_of_trains
{
int train_no;
char train_name[25];
char boarding_pt[25];
char destination[25];
int first_seats;
int fare_first;
int second_seats;
int fare_second;
char date[20];
};
info_of_trains e;
cout<<"TRno. TRname B.pt D.pt F.seats F.fare S.seats F.second Date\n";
FILE *fp;
fp=fopen("database","r");
if(fp==NULL)
{
cout<<"failure";
}
else
{
while(fread(&e,sizeof(e),1,fp)==1)
{
printf(e.train_no,e.train_name,e.boarding_pt,e.destination,e.first_seats,e.fare_first,e.second_seats,e.fare_second,e.date);
cout<<"-------------------------------------------------------------------------------\n";
}
fclose(fp);
}
}
This is where execution stops :!
You seem to have hit a breakpoint, or your program had an access violation (reading an illegal pointer). You also seem to have maximized/detached the debugging panels. You can reattach the panel by dragging the yellow bar at the top to the lower part of the screen.
Did you recieve a warning message before it happened? Otherwise, did you define a breakpoint (clicking in the left margin of the code editor, so a red circle appears there)
EDIT: As pointed out in the comments, the error occurs because you use printf the wrong way. Use cout instead, as you did above:
cout << e.train_no <<" " << e.train_name << " " << e.boarding_pt << " " << e.destination << " " << e.first_seats << " " << e.fare_first << " " << e.second_seats << " " << e.fare_second << " " << e.date << endl;