Incorrect output [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
i have corrected the errors in the program below, but am still getting mixed output. the updated results are below. Please advise.
I would appreciate any help. The relevant code sections are:
string student::standing() const
// post: use selection to return the current standing as either
// Freshman, Sophomore, Junior, or Senior.
{
cout << "Freshman";
if (my_credits <= 60.00);
{cout << "Sophomore";}
if (my_credits <= 90.00);
{cout << "Junior";}
if (my_credits >= 90.00);
else
{ cout << "Senior";}
}
string student::name() const
// post: return the student's name
{
{return my_name;}
}

Uhm, first of all your title references one error and your question shows another. Both are legitimate so let's fix both, shall we?
The thing is the C4716 error message tells you exactly what the error is: you just have to read it. It says that the function student::standing() claims to return a string but doesn't. Of course, that's not the only issue. The other problem, which you claim is C2134 (but is likely C4390) is caused because you you put a semicolon after you if which isn't right. This code:
if(my_credits <= 60.00);
{cout << "Sophomore";}
is the same as this code:
if(my_credits <= 60.00)
/* do nothing if my_credits are less than or equal to 60 */
{
/* and then always print "Sophomore" */
cout << "Sophomore";
}
Chances are that what you really want is this:
string student::standing() const
{
if(my_credits >= 90.00)
return "Senior";
if(my_credits >= 60.00)
return "Junior";
if(my_credits >= 30.00)
return "Junior";
return "Freshman";
}
For future reference, if you had searched on Google for C4390, you would have landed on an MSDN page describing this error which includes an exact description of the issue, along with the very mistake you made.
Similarly, if you had Googled for C4716, you would have landed on another MSDN page describing that error which again includes an example that's very close to what your mistake.
Finally, in this code:
string student::name() const
{
{return my_name;}
}
What is the point in those extra curly braces? They don't really hurt anything, but they just serve no conceivable purpose, except to confuse. Remove them:
string student::name() const
{
return my_name;
}

Related

How to create a search function in this C++ program [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm writing a small program with a command-line interface. In this program I want to have a search function which I managed to create.
But it for a character search for the first name but I want to create the same function which will be able to search using the "Student Registration number".
The Search Case where I'm having problems:
int kWord;
stdDetails stdFind;
cout<<"Enter the Student Registration Number of the Student: ";
cin>>kWord;
for(int x=0;x<i;x++){
stdFind = stdDetailsStructs_0[x];
if(!strcmp(kWord,stdFind.stdNum)){
search=1;
break;
}
}
if(search==1){
display(stdFind);
}else{
cout<<"Student details not found please try again."<<endl;
}
I don't think kWord should be int as the student registration numbers should be strings. If they are numbers you should use == for checking equality.
Where is search declared?
If it's a global, you should have
if(search==1){
display(stdFind);
}else{
cout<<"Student details not found please try again."<<endl;
search = 0; // <-- add this
}
stdNum is of type int in structure stdDetails.So use == operator insted of strcmp()
int kWord;
cout<<"Enter the Student Registration Number of the Student: ";
cin>>kWord;
for(int x=0;x<i;x++){
if(kWord==stdDetailsStructs_0[x].stdNum)
{
search=1;
break;
}
}
if(search==1){
display(stdFind);
}else{
cout<<"Student details not found please try again."<<endl;
}
If its just a number why cant you use == operator instead of strcmp.
use--- if (kWord == stdFind.stdNum),,,,
strcmp() is for String Comparison.
kWord and stdNum are integer values.
I would use strncmp() (even though it's really C style and you could use the STL) because it is probally failing all the time because of the '\n' at the end of the line.
if stdNum is actually a string:
if(!strncmp(kWord,stdFind.stdNum, strlen(stdFind.stdNum))){

"Access violation reading location 0x0000000c" error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Code fragments:
-inserting instances into object manager
Object* o;
for(int i=0; i<10; i++)
{
o = new Tile(32*i, 0);
o->ID = i+2;
o_manager.Create(i+2,o);
}
o = new Player(50.,50.);
o->ID = 1;
o_manager.Create(1,o);
-inserting instances from manager into quadtree
CollisionDetector = new QuadTree(0, bounds);
for(std::map<unsigned int, Object*>::iterator it = Instances.begin(); it != Instances.end(); it++)
{
std::cout << it->second->COL << std::endl;
if(it->second->COL) //probable place of error
{
std::cout << "Object (" << it->first << ")... ";
CollisionDetector->insert(it->second);
}
std::cout << "inserted into QuadTree" << std::endl;
}
I've got a problem, which cause is completly unknown for me. I've got an object manager class in which I put 11 instances (into map container). Then every step it would pass instances with collision flag set to the quadtree list.
After executing I receive an error: "Access violation reading location 0x0000000c".
From what I get from stdout, error shows up while inserting 11-th instance into the quadtree (stdout prints flag value, but doesn't print "Object (ID)..."). When I reduce number of instances to 10 orless everything works fine.
I'd be grateful for any advices, because I have no idea where to find solution.
Sorry for my english, I'm not native speaker.
"I've got an object manager class in which I put 11 instances (into map container)."
But that's just 10 instances:
for(int i=0; i<10; i++)
If this doesn't solve your problem, you should post more code.
The problem you have is for sure a pointer that is null. The 0x0000000c is the address of a member relatively to the start address of an object. (probably COL, if you give us the header for Object)
Make sure the elements you add to the map are not null.

Input validation for a c-string that is passed by reference [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
#include "cstack.h"
#include <iostream>
#include <cstring>
using namespace std;
bool isValidExpression (CStack&, char*);
int main (void)
{
char expression[21];
expression[0-21]=0;
cout<< "Enter an expression: ";
cin >>expression;
CStack stack1;
if (isValidExpression (stack1, expression)==true)
{
cout << "\nIt's a valid expression";
}
else
{
cout << "\nIt's NOT a valid expression";
}
return 0;
}
bool isValidExpression (CStack& stackA, char* strExp)
{
for(int a=0;a<21 && strExp[a]!=0;a++)
{
if(strExp[a]="}"||"]"||")") //This is the issue right here
{
cout<<"Action A" <<endl;
}
else
{
stackA.push(strExp[a]);
}
}
return true;
}
The problem I am having is that whatever input I put in, Action A always occurs. If I input [ for example, I still get action a which is not the desired result. I have always used strings for things like this, but we are required to use cstring in this program. How would you edit this to make it work?
Try to update:
if(strExp[a]="}"||"]"||")")
to:
if(strExp[a]=='}'|| strExp[a]==']'|| strExp[a]==')' )
if(strExp[a]="}"||"]"||")") will always be true because this uses "]" and ")" as the booleans to the || operators, and the string constants themselves resolve to non-zero const char *s which are considered true. Additionally, the single = is assignment, not comparison, this means all three parts of the if condition are true
The above condition reads as:
if (the lower 8 bits of the pointer generated for"}"[true]
or the pointer generated for "]" is nonzero [true]
or the pointer generated for ")" is nonzero [true])
what I think you mean to do is
if(strExp[a]=='}' || strExp[a]==']' || strExp[a]==')')
also note that the double quotes are replaced with single quotes here. Use single quotes when examining a single character rather than double quotes. Also use == for comparison.
If you're compiling with g++ you should enable warnings with g++ -Wall -Wextra, any sane compiler would have generated warnings on that line for all the reasons stated.

Loop without for, while or goto [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In Windows, one can use structured exception handling in C to write a pseudo-loop that prints all numbers from 1 to 1000 like this:
int n = 0;
__try {
*(int *)0 = 0;
}
__except(printf("%i\n", ++n), n < 1000 ? -1 : 1) {
}
I wonder if there are also other ways in C/C++ to create a loop that isn't trivial to detect if you search the code for the usual suspect keywords for, while and goto.
In C++, a simple lambda can do that:
std::function<void(int,int)> print = [&](int from, int to)
{
std::cout << from << " ";
if ( from < to ) print(++from, to);
};
print(1, 1000);
It will print all integers from 1 to 1000. And it doesn't use for, while or goto.

Expectation of "{" errors? Along with expected unspecified ID erros? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Okay I know this is more text than you want but I literally cannot find the problem in the following code
#include <iostream>
using namespace std ;
int main(void)
{
int score1, score2;
PrintInfo() ;
score1 = GetScore(); // Call function GetScore, to obtain a value for score1.
if (RangeError()) ; // IF score1 is out of range, print an error message.
{
cout << "Sorry you entered an invalid score." << endl ;
}
else // THIS IS LINE 46, THIS IS THE ELSE IT IS TALKING ABOUT
{
score2 = Getscore ; // Call GetScore a second time, to obtain a value for score2.
if (RangeError ()); // IF score2 is out of range, print an error message.
{
cout << "Sorry you entered an invalid score." << endl ;
}
else // ELSE, using a call to function maximum within a cout
{ // statement, print the maximum score.
cout << maximum() << endl ;
}
}
return 0;
}
There's a lot in there, I know, but the main problem seems to be around my second else. For whatever reason I keep getting the following errors:
lab05a.cpp:46: error: expected `}' before 'else'
lab05a.cpp: At global scope:
lab05a.cpp:46: error: expected unqualified-id before 'else'
I know it's something to do with bracketing but I can't find any problems! Also: I apologize for the lack of line numbers, we are not using an editor that allows them. I could have added them, but I removed a lot of irrelevant text for brevity and adding them on would not have given them the proper line number for the error. As such I will show where line 46 is. I removed most of the declarations since the problem does not seem to lie therein. Please help me, this is the first time I've ever had a problem I couldn't solve on my own.
if (RangeError()) ;
{
should be
if (RangeError())
{