Hi all firstly I would just like to say what a great site, the documentation on hexadecimal and binary have been a great help.
I have just started coding a few days ago and I am currently working my way through Bjarne Stroustrup's Programming, Principles and Practice. It is taking a while but it is great fun.
I have however run into my first problem that I cannot solve myself and have spent the last few hours searching for fixes, such as cout problems, windows updates, visual studio cout problems etc. I have also downloaded another complier but I am still trying to get Hello World! to work on that :)
The problem is that in visual studios 2013 the following code pops up a blank terminal, I can however using cout to show Hello World. This code is from page 81 of the book if that helps any.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "std_lib_facilities.h"
int main()
{
double d =0;
while (cin>>d) { // repeat the statements below
// as long as we type in numbers
int i = d; // try to squeeze a double into an int
char c = i; // try to squeeze an int into a char
int i2 = c; // get the integer value of the character
cout << "d==" << d // the original double
<< " i=="<< i // converted to int
<< " i2==" << i2 // int value of char
<< " char(" << c << ")\n"; // the char
}
}
Assuming that you're not getting any compilation errors or warnings, and that custom header file of yours includes the MS obligatory "stdafx.h" or at least mimics its contents, your code looks fine. Try starting a new project in its own folder and recompile your code. Sometimes when you constantly reuse projects for completely different code old stuff from the object files gets left behind and breaks things in the strangest ways.
EDIT: @ coder777: I really hope that isn't the case but thank you for making me laugh.
and they say there is no such thing as a stupid question, I was changing the double d= value to see if that was how to get a different input, thanks so much think I need to take a break from the screen :). Thank you for taking the time to respond
thanks for the tip, I have been playing around with the code (didn't know what was prompting me to enter something) Just to be 100% sure I enter the D value and while is in fact a loop and I know the prompt by cin ? Does this make any sense ?
cin >> d // std::cin is the standard input stream used with "narrow characters" (ASCII)
It sends the input to the variable double d.
If cin encounters an invalid character which is not accepted as double, it sets an error flag which makes std::cin return false for its value.
this is the reason why you can place cin in the while loop.
while (true) ; loops forever until true becomes false...
So, while (std::cin >> d) loops until cin encounters an end-of-file (ctrl-Z in windows, ctrl-D in UNIX), a logical error in the stream (an 'a' character instead of a number), or a stream disruption happens (serious error!).
#include <iostream>
int main()
{
double d = 0;
std::cout << "Enter numbers i.e. 67.45, 6.8e1, 'D'" << std::endl;
while (true) // repeats forever, but we will check std::cin with the if statement
{ // to break out of the loop
std::cin >> d;
if (!std::cin) { // here std::cin returns false
// this is only an output to show what std::cin returns if it fails
std::cout << "exiting...\n"
<< "std::cin.good(): " << std::cin.good() << ":" << std::cin.goodbit << std::endl
<< "std::cin.eof(): " << std::cin.eof() << ":" << std::cin.eofbit << std::endl
<< "std::cin.fail(): " << std::cin.fail() << ":" << std::cin.failbit << std::endl
<< "std::cin.bad(): " << std::cin.bad() << ":" << std::cin.badbit << std::endl
<< "std::cin.rdstate(): " << std::cin.rdstate() << std::endl;
break; // breaks out of the while loop if std::cin fails
}
// repeat the statements below
// as long as we type in numbers
int i = d; // try to squeeze a double into an int
char c = i; // try to squeeze an int into a char
int i2 = c; // get the integer value of the character
std::cout << "d==" << d // the original double
<< " i==" << i // converted to int
<< " i2==" << i2 // int value of char
<< " char(" << c << ")\n"; // the char
}
return 0;
}