Input to Visual Studio Console Window not showing - c++

I've recently started to learn how to code, and started with C++. The IDE I'm using is Visual Studio Community 2019 on Windows 10 64-bit. I wrote a small code which I wanted to debug using breakpoints. When Visual Studio enters debug mode it gives the following message on the bottom-left corner: "loading symbols for kernel32.dll" (the message isn't restricted to kernel32.dll, but also ntdll.dll, KernelBase.dll, msvcp.dll, vcruntime140d.dll, ucrtbased.dll). I opened the module tab and automatically loaded all of them (I don't really know what these are, some clarity in this regard would also be appreciated), however, the main issue persists: when I enter a keyboard entry to the console it doesn't show the character entered. After pressing step-into it shows the value that has now been stored inside the declared variable and the entered value on the console, which may or may not have been entered correctly as the character input is invisible in the console window while typing.
Does this have something to do with Stack, Heap management, RAM etc.? Please explain what this issue means and what I can do to solve it as there is no point in debugging if i cannot see what is being printed or entered on the console.
The code is shown below, if it is required (The program takes an input from the user and compares it to a randomly generated number as shown):
#include <iostream>
#include <time.h>
#include <stdlib.h>
int main()
{
int Guess, RandomNumber,End=1;
srand(time(NULL));
RandomNumber = rand() % 100 + 1;
std::cout << "Enter a Number. (HINT: The number lies between 0 and 100!)";
while (End != 0) //this is where I added a breakpoint.
{
do
{
std::cin >> Guess;
if (Guess > RandomNumber)
{
std::cout << "You Guess is incorrect. The special number is smaller than your guess! ";
}
if (Guess < RandomNumber)
{
std::cout << "You Guess is incorrect. The special number is larger than your guess! ";
}
} while (Guess != RandomNumber);
std::cout << "You are CORRECT! The number was: " << Guess<<std::endl<<"Press 1 to play again. To exit press 0"<<std::endl;
std::cin >> End;
}
}

Related

Why c++ exe files closes immeditely? [duplicate]

This question already has answers here:
What is the Best Practice for Combating the Console Closing Issue?
(11 answers)
Closed last month.
I have 1 simple code in c++. That code calculate sum of 2 entered numbers. I convert that code to .exe for running windows. My problem is it will work but it doesn't show sum. After entering second number program closes immediately. I have no idea why this happen.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Please enter first number \n";
cin >> a;
cout << "Please enter second number \n";
cin >> b;
int z = a + b;
cout << "Sum of these numbers are: " << z;
}
I find solution via command prompt. But I want to completely run my .exe file without command prompt
Closing the console after program finishes is a normal behaviour when you don't start it from the terminal. You could delay the closing of console by using
std::cin.get(). The function waits until recieving input from terminal, so when you press any key, the program will end and console will close

[C++]Eclipse ignores console input during debugging

During debugging Eclipse doesn't "see" INPUT from built-in console, just ignores it. Simple example:
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << a << endl;
cin >> a;
cout << a << endl;
return 0;
}
works perfectly fine when just run, but when I try to debug it, first "data on input"(?) is always a number around 40, and only zeros next, no matter what i will write to console.
So program executes, first variable is set to ~40 and all next to zeros.
Output works fine, values are written to console, only input doesn't work.
I work on Windows 10 and use MinGW.
Thanks in advance.
#EDIT
Everything works, when I use native Windows console
(.gdbinit file with set new-console on line)

Visual C++ - Runtime Check Failure #3 - Variable is not initiliazed

I use Visual C++ 2010 Express Edition to compile and run the .exe files I write in the C++ programming language. I am trying to create a loop-based logic using C++ to ask the user how many entries he chooses to enter, and ask questions limited to that no. of entries. For example I want to output, "How many characters do you wish to enter?: " Say the user gives the answer as '3' which is stored in the int variable 'entries'. I then want to keep asking the question 3 times before it stops and continues with the next line of code. I hope you understand, here is a block of code to demonstrate what I am doing:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "How many values do you need to enter?: ";
int entries;
cin >> entries;
int offset, number;
string valueName[50];
float valueValue[50];
for (offset = 0; offset < entries; offset++)
{
cout << "Enter " << number << " Value Name: ";
cin >> valueName[offset];
cout << "Enter " << valueName[offset] << "\'s value: ";
cin >> valueValue[offset];
for (number = 1; number <= entries; number++)
{
}
}
char response;
cin >> response;
return 0;
}
Strangely when I run this simple program, it fails when I enter the value's name to be inserted into the 0th element of the valueName[] array. It just pauses the execution of the program and a dialog box pops up saying "Runtime Check Failure #3 - Variable 'number' is being used without being initialized!" Another problem regarding this program is that, for quite some time, when I ran this program this "Runtime Check Failure #3" box never appeared, and when it didn't, the number value went wrong, and first started with 1, and then for the next loop jumped to 6, and then repeated 6 again for the next loop! Please help me! I've checked online scouring this problem everywhere, but it just doesn't apply to my type of problem! Is it because the variables are out of scope? But they're declared outside the for loops right? So please help me!
The runtime is telling you the truth, the following line comes after you have declared number as an int but have not given it a value.
cout << "Enter " << number << " Value Name: ";
In your code you declare the following, in C++ this means give me 2 ints but the values are not defined yet, e.g.
int offset, number;
Change it to something like this ..
int offset = 0;
int number = 0;
You are printing the variable number without assigning to it first, i.e. it's uninitialized. When it prints some random number it's because that what happens to be in the memory at the time you run the program. Assign a value to it before you use it.
The problem is exactly the error message you're getting. You're using the variable number without initializing it.
You use the variable right here, at the top of your loop, when it hasn't been initialized to anything yet:
cout << "Enter " << number << " Value Name: ";
What is your intention with the number variable? It doesn't really seem to be serving any purpose. If you want to print which entry you're currently on, you could use the offset variable instead, like this:
cout << "Enter " << offset << " Value Name: ";
But that still seems a little unclear to me.
But the reason that you're having a problem is because the value is uninitialized, so you're experiencing undefined behavior. This is also the reason that Visual Studio doesn't always catch it; it will probably always catch in Debug mode, but in Release mode it will almost never catch it. You need to initialize all your variables before you use them.
In my case it was because an extern variable was declared twice.

C++ For loop loops only 299 times

I'm having this weird problem. My code is simple:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "number: ";
cin >> num;
for (int i=0;num>i;i++) {
cout << i <<"\n";
}
system ("Pause");
return 0;
}
If the input for example is 1000, the output contains numbers from 701-999.
Any idea?
I'm using Dev-C++ IDE on Parallels.
Actually it prints all of them, from 0 to 999, but your console's buffer is not large enough. So you see only the last part. if you print into a file, not the console, you'll see :)
The loop ends when num>i is no longer true. This occurs when i is 1000, so the last loop executed will be with value 999. As for not seeing lower than 701, maybe your screen buffer is too small.
It will start with 0-999. Also, it appears to you that it starts with 701 because of your console screen settings. If you want to see it for yourself, change the newline into a space:
cout << i <<" ";
Did 0-700 scroll off the screen? Run your exe like this
your_program > out.txt
Then look at out.txt in an editor.
Works absolutely fine for me. I'd suggest your IDE might be playing tricks on you. Could you redirect output into a file and check that?
Regarding #JoshD answer,
You will need to:
for (int i=0;num>=i;i++) {
cout << i <<"\n";
}

Visual C++ holding the console open, strange behavior

I've searched around here first, and seen many solutions to making Visual C++ hold the console open at the end of program's execution, but I'm getting some weird behavior from one of the methods, and I don't understand where this behavior is coming from.
So (a) what's the best method (besides crtl-F5 and setting subsystem to console) to hold the console open?
and (b) why am I getting the strange behavior that I will now elaborate on?
Here is the code
/*
Learning how about variables and accepting inputs
*/
#include <iostream>
using namespace std; // cout and cin from the standard namespace.
// using std::cout; -- alterative declarations
// using std::cin;
int main()
{
char first, last;
cout << "Please enter your first and last names:\n";
cin >> first; // get one char
cin.ignore(256,' ');
cin >> last; // get one char
cout << "Your first and last initials are " << first << last;
cin.ignore('\n');
return 0;
}
The strange behavior is, I have to press the Enter exactly 4 times before it will exit the console.
Why 4 times?