It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have the following code
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "graderec.h"
int main( )
{
GradeRecord studentAnn("45-2791", 14, 49);
GradeRecord studentBob("67-5803",25, 50);
int bobsUnits;
int bobsGradePoints;
int annsUnits = 4;
int annsGradePoints = 16;
cout << "Ann's Grade Information:" << endl;
studentAnn.writeGradeInfo();
cout << endl;
cout << "Bob's Grade Information:" << endl;
studentBob.writeGradeInfo();
cout << endl;
cout << "Enter Bob's units: ";
cin >> bobsUnits;
cout << "Enter Bob's grade points: ";
cin >> bobsGradePoints;
cout << endl;
cout << "Bob's Grade Information:" << endl;
studentBob.updateGradeInfo(bobsUnits, bobsGradePoints);
studentBob.writeGradeInfo();
cout << endl;
cout << "Ann's Grade Information:" << endl;
studentAnn.updateGradeInfo(annsUnits, annsGradePoints);
studentAnn.writeGradeInfo();
system("PAUSE");
return 0;
}
void asterisks()
{
cout << "************************************************************************" << endl;
}
I need to use a free function to display about 60 asterisks where I have cout << endl. I followed the example that I was giving but can't get it to work.
The code below is the example that I was given on how a free function looks.
void companyBanner()
{
cout << *************************** << endl;
cout << ** Tech Guys LLC ** << endl;
cout << *************************** << endl;
cout << endl;
}
Updatea: Got it working, thanks for the help everyone. I rewrote the free function and added asterisks() above the main again and it worked. Must have been something in the free function that was causing it to not work.
You should call the function you defined otherwise it will never be executed.
You should also place either a declaration or the whole definition of the function before you call it for the first time.
String literals should be enclosed in double quotes ".
'Doesn't work' is too vague for us to help you. My attempt for now is that you have not prototyped your asterisks() function. Put void asterisks(); above your main.
If I understand the question (please tell me if I'm wrong), just replace cout << endl; by a call to your function: asterisks().
Also, either move the function asterisks before your main, or add the prototype void asterisks(); above the main.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
cout << "*****************************************" <<
endl <<
cout << "Hello All!" <<
endl <<
cout << "Welcome to CSCI-111!!!!!" <<
endl <<
cout << "It is great to see you!" <<
endl <<
cout << "*****************************************" ;
return 0;
}
The first cout is fine and outputs correctly, but every cout after that outputs a strange string of numbers before the words in quotation marks (0x600e88) and my output ends up looking like this
*****************************************
0x600e88Hello All!
0x600e88Welcome to CSCI-111!!!!!
0x600e88It is great to see you!
0x600e88*****************************************
What you currently have:
cout << "blah" << endl << cout << "blah" << endl << cout << ... ;
// ^~~~ ^~~~
You're printing the cout itself, this is what gives you weird numbers.
What you should have:
cout << "blah" << endl;
cout << "blah" << endl;
Or:
cout << "blah" << endl
<< "blah" << endl;
Terminate endls:
cout << "*****************************************" << endl;
cout << "Hello All!" << endl;
cout << "Welcome to CSCI-111!!!!!" << endl;
cout << "It is great to see you!" << endl;
cout << "*****************************************" ;
Or delete the redundant couts:
cout << "*****************************************" << endl <<
"Hello All!" << endl <<
"Welcome to CSCI-111!!!!!" << endl <<
"It is great to see you!" << endl <<
"*****************************************" ;
Otherwise, the expression continues, and you print cout itself, and since it's a function pointer, you print it's address (0x600e88).
In the sequence
cout << "Something" << endl << cout;
the first cout indicates the start of ostream (a stream printing out to the console), while the second one is the part of the stream you want to output, and is treated as a pointer, who outputs what he contains - a numeric address of the call to cout.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i'm having an issue with my C++ code:
#include <iostream>
#include <cmath>
#include <string>
#include <windows.h>
using namespace std;
void displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints) // Call this function to display
{ // ennemy related informations
cout << endl << "Ennemy health points: " << ennemyHealthPoints;
cout << endl << "Ennemy attack points: " << ennemyAttackPoints << endl;
}
int main()
{
//Player related variables
int healthPointsMax(100);
int actionPointsMax(100);
int recoveryPoints(100);
int healthPoints(100);
int actionPoints(100);
int attackPoints(100)
//Player related variables
//Ennemy related variables
int ennemyHealthPoints(230);
int ennemyAttackPoints(10);
//Ennemy related variables
//Main variables
string stringInput;
//Main variables
//TEXT
cout << "HP: " << healthPoints << endl;
cout << "AP: " << actionPoints << endl;
cout << "RP: " << recoveryPoints << endl;
cout << endl;
cout << "HP = Health Points, AP = Action Points, RP = Recovery Points" << endl;
cout << endl;
cout << "CONTROLS:" << endl;
cout << "attack [ennemy name] //attacks the ennemy" << endl;
cout << "heal [playername] // heals the selected player" << endl;
cout << endl;
cout << "A wild nugget appears!" << endl;
cout << endl;
cout << "What do you want to do?" << endl;
// TEXT
getline(cin, stringInput);
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);
}
return 0;
}
The debugger says that there is a problem with the fonction "void displayEnnemyStatus"
I checked every variable, there is no problem with that.
Am I doing something wrong?
Try :-
if (stringInput == "attack ennemy")
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
**displayEnnemyStatus(ennemyAttackPoints, ennemyHealthPoints)**;
}
Instead of :-
if (stringInput = attack ennemy)
{
cout << endl << "You dealt 100 attack points to: ENNEMY" << endl;
ennemyHealthPoints = ennemyHealthPoints - attackPoints;
displayEnnemyStatus(ennemyHealthPoints, ennemyAttackPoints);
I have been trying to test build this old 'text adventure' thing I found,
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
/////////////////////////////////////////////////////// VARIABLES //////////////////////////////////////////////////////////
string name;
string shipName;
int health;
int crewCount;
int armyTotal, activeArmy;
int casualtiesCount, woundedCount, healedCount;
// 'food' as in a whole meal (beverage, chewables, etc)
int foodCount;
////////////////////////////////////////////////////// INTRODUCTIONS ///////////////////////////////////////////////////////
cout << "What is thy name?\nName: ";
cin >> name;
cout << endl << "What will you name your ship?\nShip Name: ";
cin >> shipName;
cout << "\nSETTING: You are floating through space on giant space cruiser " << endl << "known as the " << shipName << ".\n You are on a random patrol sorti, just looking out for any trouble...";
cout << "Press ENTER to continue...";
cin.get();
cout << "\nFrom here on out, type corresponding number to which choice you want to make.\nPress ENTER to continue...";
cin.get();
//////////////////////////////////////////////////////// BEGINNING ////////////////////////////////////////////////////////
cout << endl << "Admiral " << name << ", we need you on flight deck.";
cout << "1: Go to flight deck.";
cout << "2: Go to kitchen.";
cout << "3: Go to MedBay.";
cout << "4: Do nothing.";
}
and I get an error for:
cin >> name;
where ">>" matches no operands.
I clearly remember this code working at some point I do believe. If I try and skip ahead, I get an error where it can't find an exe (and there is no option to Build Final)
Sorry for not being clear, but I haven't used C++ for a few years now, quite rusty on just about everything. Any sort of wisdom to shed?
You need to #include <string>. That's where the actual operator is defined. Chances are that in the past, <iostream> may have included it perchance, which it's allowed to but not required (or guaranteed) to.
I'm trying to learn how to use cin and getline to write a paper grading program that I can use at school. It's kind of a tricky project for a beginner but it lets me know what I need to learn and this is the first thing I need to do.
int main()
{
string grader;
int x;
cout << "Who will I be assisting today? ";
getline (cin, grader);
cout << "Hello " << grader << ".\n";
cout << "How manny questions are on the test you will be grading? ";
getline (cin, x);
cout << "this is a " << x << "question test graded by" << grader << ".\n";
}
Lets say I answered John Doe for the first question, then 20 for question two. I want it to print "this is a 20 question test graded by John Doe"
Where am I going wrong?
I'm sure it's a stupid mistake but it's bugging me. I'm a novice so sorry for the ignorance. I will have more questions regarding this program that will not have to do with user in-put. is it ok to post these questions here, or start new topics?
thanks
Since you did not state what was your error, and alas it could also be a missing include/namespace.
The complete runnable/compilable program would be:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string grader;
int x;
cout << "Who will I be assisting today? ";
getline (cin, grader);
cout << "Hello " << grader << ".\n";
cout << "How manny questions are on the test you will be grading? ";
cin >> x;
cout << "this is a " << x << "question test graded by" << grader << ".\n";
}
anyway, this will immediatly close after you entered the amount of questions (or you call your exe from a shell/cmd) - so dont wonder if you cant see the result.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Just making a small program to begin c++ and the compiler says that there is an else without an if in reference to the while loop in main, though it is clearly not the case and I cannot see why. It works fine if I remove the while loop.
#include <iostream>
using namespace std;
int number;
int arithmetic(int num)
{
if(num > 20)
num = num * 5;
else
num = 0;
return (num);
}
int main()
{
int wait;
cout << "I will take any number providing it is higher than twenty" << endl;
cout << "and I will multiply it by 5. I shall then print every number" << endl;
cout << "from that number backwards and say goodbye." << endl;
cout << "Now please give me your number: " << endl;
cin >> number;
int newnum = arithmetic(number);
if (newnum != 0)
cout << "Thank you for the number, your new number is" << newnum << endl;
while(newnum > 0){
cout << newnum;
--newnum;
}
cout << "bye";
else
cout << "The number you entered is not greater than twenty";
cin >> wait;
return 0;
}
You are missing brackets. You have
if (newnum != 0)
cout << "Thank you for the number, your new number is" << newnum << endl;
while(newnum > 0){
cout << newnum;
--newnum;
}
cout << "bye";
else
cout << "The number you entered is not greater than twenty";
while you should have:
if (newnum != 0)
{
cout << "Thank you for the number, your new number is" << newnum << endl;
while(newnum > 0){
cout << newnum;
--newnum;
cout << "bye";
}
else
cout << "The number you entered is not greater than twenty";
If you have more than one operation in if statement, you should always use brackets. If you have just one, you can as well omit them (as in this "else" statement).
You need a { after if (newnum != 0) and a } before the else.
This type of construction is wrong:
if(something)
line1;
line2; // this ; disconnects the if from the else
else
// code
You need something like
if ( something ) {
// more than one line of code
} else {
// more than one line of code
}