Multiple Errors When Writing C++ in Sublime Text 3 - c++

Let me preface this by saying I have searched (for a few days now) for answers to these questions and can't find anything that solves the problem. In fact, I think I've only made it worse.
Also, when it comes to programming, I am a complete beginner and am teaching myself C++ (I know, I know, you're not supposed to start with C++ as your first programming language).
I know in the StackOverflow guidelines it says to describe your problem first before posting any code but part of my problem is that I don't even understand what the problem is...
For reference, I'm running Windows 7 64-bit and writing my code in Sublime Text 3 (build 3059) and, well, I'm not entirely sure what I'm compiling it with, I mean I have gcc installed(?) but I'm thinking that's part of the problem- I installed something called Cygnus as well as MinGW and Visual Studio Express 2012 but I'm not sure if these are actually compilers/I simply don't know what they do. Also, I've modified my Environment Path variable several times which was probably a bad idea.
Anyway, here is the code for the first of the two problems:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
return 0;
}
When I "build" the code, it works fine, however when I attempt to run it, this is the error that I get:
bash.exe: warning: could not find /tmp, please create!
[sig] C:\cygnus\cygwin-b20\H-i586-cygwin32\bin\bash.exe 1020 (0) call_handler: couldn't get context of main thread, error 998
I've searched for how to add/create bash but truthfully did not understand the answers that I found.
Here is my second problem. I'm learning C++ from Stroustrup's "The C++ Programming Language, 4th Edition" which, for several chapters, asks the reader to include a header ("std_lib_facilities.h") which I have saved and placed in the folder along with my practice files. This is some code I've written myself (which is probably all wrong) but even copying code from the book and running it generates this error. (I've tried just using #include and using namespace std; however things still don't work.)
//convert from miles to kilometers. 1.609km in 1 mile
#include "std_lib_facilities.h"
int main()
{
cout >> "Please enter a length in miles: " >> endl;
float miles = 0;
cin << miles;
float kilometers = 1.609;
float result = miles / kilometers;
cout >> miles >> " miles is equal to " >> kilometers >> endl;
return 0;
}
The error it generates when I attempt to build it is extremely long so I'll post a snippet of it:
In file included from c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64- mingw32/4.7.0/../../../../include/c++/4.7.0/ext/hash_map:61:0,
from C:\Users\brekki\Desktop\CPP\MINE\std_lib_facilities.h:21,
from C:\Users\brekki\Desktop\CPP\MINE\mtokm.cpp:2:
c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64- mingw32/4.7.0/../../../../include/c++/4.7.0/backward/backward_warning.h:33:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]
C:\Users\brekki\Desktop\CPP\MINE\mtokm.cpp: In function 'int main()':
C:\Users\brekki\Desktop\CPP\MINE\mtokm.cpp:5:10: error: no match for 'operator>>' in 'std::cout >> "Please enter a length in miles: "'
C:\Users\brekki\Desktop\CPP\MINE\mtokm.cpp:5:10: note: candidates are:
In file included from c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64- mingw32/4.7.0/../../../../include/c++/4.7.0/string:55:0,
from c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64- mingw32/4.7.0/../../../../include/c++/4.7.0/bits/locale_classes.h:42,
from c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../include/c++/4.7.0/bits/ios_base.h:43,
from c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64- mingw32/4.7.0/../../../../include/c++/4.7.0/ios:43,
from c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64- mingw32/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from c:\users\brekki\gcc\bin\../lib/gcc/x86_64-w64- mingw32/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from C:\Users\brekki\Desktop\CPP\MINE\std_lib_facilities.h:4,
from C:\Users\brekki\Desktop\CPP\MINE\mtokm.cpp:2:
When I attempt to run the program, I get a similar error except it is preceded by the "bash.exe: warning:"
My ideas as for what the problem(s) could be are:
I've somehow screwed up my Path variables
My GCC files are outdated
Trying to use too many compilers
Bad Sublime Text 3 C++ build
Stroustrup's header causing obvious issues
Sorry for the long, possibly stupid question. Any insight at all would be extremely appreciated.

First at all, MinGW and Cygnus are just installer for the GCC compiler. As Preet Kukreti allready commented, you should try an IDE like Visual Studio Express or otherwise if you didn't want the Microsoft thing you should try CodeBlocks which is also free.
I wouldn't recommend to learn C++ on Windows without a IDE. When you want to programm C++ with a text editor and the command line, you should probably go with linux.

You may not know yet how to setup a compiler, so to make it easier for you, I recommend you to use an IDE, like Code::Blocks. It comes with a compiler (MinGW) so you don't have to install one by yourself. There are other IDE for C++, but this one is the most complete and easy-to-us I know.
Your problem with the examples from your book is that you have inverted the << and >> signs :
stream << variable goes with an output stream like cout, it won't work with an input stream whereas stream >> variable will only work with an input stream like cin.
That means your code should look like this :
//convert from miles to kilometers. 1.609km in 1 mile
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter a length in miles: " << endl;
float miles = 0;
cin >> miles;
float kilometers = 1.609;
float result = miles * kilometers; //result in kilometers = length in miles * 1.609 (not /)
cout << miles << " miles is equal to " << result /*You want the result here, not the length of 1 mile in kilometers*/ << " kilometers" << endl;
return 0;
}

Related

Codelite build presenting only the last row of output

Using CodeLite c++14, mingw-32. I'm fairly new and learning from a course to program in the languge, but i've encounterd a problem in the last couple of days i couldn't figure out. For some reason, the command line prompt shows only the last row of my output code. this happens in all types of code that i've done so far.
Something that might be connected is that sometimes on the first building & executing of code (after a clean) i'm getting an error:
mingw32-make.exe: *** [All] Error 2
It's just an inconvinice though, a second build is solving this problem for some reason. But it might be connected some how.
Any way here's an example for a code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
char vowels[] {'a','e','i','o','u'};
cout << "\nThe first vowel is " << vowels[0] << endl;
cout << "\nThe last vowel is " << vowels[4] << endl;
return 0;
}
The output that shows is only "The last vowel is u", it goes to basiclly any code i create.
Thanks!
So I found an answer, and this probably won't help alot of people because its pretty absurd. apperantly i had a mining virus on the computer which also made it so at startup only a CMD window would appear and I had to type 'explorer.exe' manually.
So mingw basiclly tried to tell me something was messing with my CMD every time i opened it. What a way to find out.
A fix for those who face the same (found on reddit #spiralspectra):
1.a) Open Run (windows key + r) and enter "%appdata%" without quotes and press enter.
1.b) Go to the Microsoft folder and delete the SoundMixer folder contained within (This is the mining software).
1.c) Open Run and enter "regedit.exe" without quotes.
1.d) Find the registry entry mentioned as([HKEY_CURRENT_USER\Software\Microsoft\Command Processor]) and remove it. This entry attempts to run the miner whenever a CMD is started.
2.a) Go to the winlogon registry entry in the same regedit software ([HKEY_CURRENT_USER\Software\Microsoft\WindowsNT\CurrentVersion\Winlogon])
2.b) Check the sub-key named "shell" and change its value to "explorer.exe" without the quotes. (This means when you log onto windows it will no longer attempt to run the miner and instead start up normally.)

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.

Coloured output in Turbo C++

My compiler is Turbo C++ v3.0 with DOS v5.0 emulated in DOSBox v0.74
I use this because Turbo C++ is the compiler with which
my highschool has chosen to teach the C++ programming language.
It has been stressed that I use this compiler while coding my final term project.
I'm running Windows 8.1 (64 bit) with Intel Core i5-3317U CPU # 1.70GHz
For the sake of liveliness and in tribute to popular culture,
I want my output screens to have green text.
The following is what seemed to work :
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
textcolor(2); // text set to green colour (conio.h function)
cprintf("\n\t Hello World"); // cprintf from conio.h
cout << "\n\t Hello World"; // cout from iostream.h
getch();
}
The output of which is as follows (screen has been trimmed to save space on this post):
According to the Help Section in Turbo C++,
cprintf() sends formatted output to the text window on the screen.
As you can see, the text printed onto the screen by cout is not green and my project is composed of a lot of cin and cout and some writing and reading files.
The result I desire can (although I have not yet tried) most likely be obtained by replacing all my cout << "..."; with cprintf("...");
but I've written so many cout statements that it's going to be hard to edit the code that much.
cprintf is new territory for me and I'm slightly set aback by how
cprintf("\t"); is outputed as o
So, I'm not to keen using this. I don't wish to use this until and unless it is my only option.
The libraries cstdlib.h and windows.h are unavailable in Turbo C++ and hence I can't use their utilities to get what I want either.
In the end, all I want is that output prompt to display the text I've couted in bright green. Minimal change to my code would be nice.
I wouldn't even mind having to change some settings of my emulator or compiler or shell to do it.
All help is much appreciated. Thank you in advance =)
Ah, the 1990s called, they want their QEMM back :)
The one solution I can think of is to put this in your CONFIG.SYS:
DEVICE=C:\DOS\ANSI.SYS
and then output ANSI escape sequences.
You can use the constream library for colored text output:
#include <constrea.h>
int main()
{
constream cout;
cout << setclr(2);
cout << "\n\t Hello, World!" << endl;
getch();
return 0;
}
I don't know what to do about the tab character.
you just need to add the clrscr(); function after the textcolor(); and it works with the couts

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.

How do I input data using the eclipse console? (c++)

I'm trying my hand at c++ and am using fedora eclipse (3.4.2) as my IDE.
At the moment I'm trying to enter a string of numbers into the console, get the program to sort them and spit them back out. The program is straight out of a book and works with xcode and through a normal terminal - so I know it's correct.
Basically I run the program and enter a few numbers into the eclipse console, the numbers are coloured green so I know its accepting the input correctly.
When I hit enter, the console jumps to a new line and nothing happens. When I press control+shift+D, nothing happens. When I press control+d, nothing happens.
I use eclipse for python too, and the console works properly. Just hitting enter inputs data to the program.
Am I missing something here? I've spent the last half hour or so trying to figure this out. Can anyone help me? Thanks.
What version of ecplise and what complier are you using? The following worked for me on Eclipse Ganymede with GCC version 3.4.5:
#include <iostream>
using namespace std;
int main() {
int x = 0;
cout << "Type your input here:";
cin >> x ;
cout << "You entered " << x << endl;
return 0;
}
How does your program know that input has ended? It sounds like it accepts multiple lines of input in the console window. Isn't there some magic case that pops you out of that loop so you can process the input that's been collected?
As other said, without the code there's no answer.