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.
Related
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.
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
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;
}
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.
Yeah. So, I'm trying to make a code for a guessing game. In this game, there's a hard mode. In hard mode, you have 15 guesses, and have to guess between 1 and 500. But my problem is this:
I'm trying to have hard mode save & display your wins/losses, but when it outputs the contents of wins.txt it outputs something like this:
Wins: 0x7fffee26df78
Losses: 0x7fffee26e178
It's really confusing me. Here's the part of the code I have for that:
ifstream losses_var("losses.txt");
ifstream wins_var("wins.txt");
losses_var>> loss;
wins_var>> win;
wins_var.close();
losses_var.close();
Then it gets called with:
cout<<"Wins: "<< wins <<"\nLosses: "<< losses <<"\n"
If you would like to see the full source code, it's here: http://pastebin.com/gPT37uBJ
My second problem:
Hard mode won't display when you win. That's pretty much the whole problem. In my code, the loop for asking users for input uses
while (guess != randNum)
So at the end bracket I have what I want the code to display when a user wins, but it just doesn't run. It just stops. I would like it if someone could help me with this. The line that has the bug is line 97 through 105. Again, source code is here: http://pastebin.com/gPT37uBJ
You've got your variable names confused
cout<<"Wins: "<< wins <<"\nLosses: "<< losses <<"\n";
should be
cout<<"Wins: "<< win <<"\nLosses: "<< loss <<"\n";
It's important to pick good variable names. One reason is so that you don't confuse yourself about what your variables mean (if you confuse yourself think how it's going to be for someone else looking at your code).
Others have already answered the output problem (win vs. wins). The other problem is probably in your logic of while loop nesting. The outer loop (while (guess != randNum)) starts, but its body contains the entire inner loop (while (guesses_left != 0)). This means that the outer condition is not checked again until the inner loop terminates, which means you've run out of guesses. Also note that if you guess correctly, inner loop will never terminate. You probably want something like this:
while (guesses_left > 0) {
// input user's guess
if (guess < randNum) {
// process it
} else if (guess > randNum) {
// process it
} else {
// it's equal, user won
// do what's necessary for a win
return 0;
}
}
// ran out of guesses
// do what's necessary for a loss
return 0;
You are not writing your variables win and loss to cout. From your pasted code, I can see that wins and losses are ofstream objects, which means you are probably seeing addresses there. I would advise you to choose more informative variable names to avoid hard to spot mistakes like this.