|
|
jschell wrote: you write 6 bytes to that you have a problem. Of course, but not an exception. Which is why you are always reminded to use the strxxx_s versions which include bounds checking. But you have to work quite hard to write past the beginning of a buffer.
|
|
|
|
|
#include <iostream>
#include <string>
#include <cstring>
#include <stdlib.h>
#define MAX_RESPONSE_LENGTH 5
using namespace std;
int main()
{
const float ENGLISH_MIDTERM_PERCENTAGE = .25;
const float ENGLISH_FINALEXAM_PERCENTAGE = .25;
const float ENGLISH_RESEARCH_PERCENTAGE = .30;
const float ENGLISH_PRESENTATION_PERCENTAGE = .20;
const float SCIENCE_MIDTERM_PERCENTAGE = .40;
const float SCIENCE_FINALEXAM_PERCENTAGE = .40;
const float SCIENCE_RESEARCH_PERCENTAGE = .20;
const float MATHS_MIDTERM_PERCENTAGE = .50;
const float MATHS_FINALEXAM_PERCENTAGE = .50;
float finalNumericGrade = 0;
char finalLetteredGrade;
char response [MAX_RESPONSE_LENGTH];
string moreGradesToCalculate;
short midtermGrade = 0;
short finalExamGrade = 0;
short researchGrade = 0;
short presentationGrade = 0;
cout << "Do you have a grade to calculate? \n";
cin >> moreGradesToCalculate;
for(int x = 0; x < moreGradesToCalculate.length();x++){
moreGradesToCalculate[x] = toupper(moreGradesToCalculate[x]);
}
while(moreGradesToCalculate == "YES" ){
cout << "Designate a student type to calculate.\n\n";
cout << "1.English 2.Science\n\n"
<< "3.Maths \n";
cin >> response;
if(strlen(response) == 0){
cout << "You must select a student type.\n" << endl;
return 0;
}
if((atoi(response) < 1) || (atoi(response) > 3)) {
cout << response << " - is not a valid student type.";
return 1;
}
switch (atoi(response))
{
case 1:
cout << "Midterm Grade : ";
cin >> response;
midtermGrade = atoi(response);
cout << "Final Exam Grade : ";
cin >> response;
finalExamGrade = atoi(response);
cout << "Research Grade : ";
cin >> response;
researchGrade = atoi(response);
cout << "Presentation Grade : ";
cin >> response;
presentationGrade = atoi(response);
finalNumericGrade = ((midtermGrade * ENGLISH_MIDTERM_PERCENTAGE) + (finalExamGrade * ENGLISH_FINALEXAM_PERCENTAGE) +
(researchGrade * ENGLISH_RESEARCH_PERCENTAGE) + (presentationGrade * ENGLISH_PRESENTATION_PERCENTAGE));
cout << endl << "**** ENGLISH STUDENT ****\n" << endl;
cout << "Midterm Grade : " << midtermGrade << endl;
cout << "Final Exam Grade : " << finalExamGrade << endl;
cout << "Research Grade : " << researchGrade << endl;
cout << "Presentation Grade : " << presentationGrade << endl << endl;
if((finalNumericGrade >= 92) && (finalNumericGrade <= 100)) {
finalLetteredGrade = 'A';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n" ;
}
else
if((finalNumericGrade < 92) && (finalNumericGrade >= 85)) {
finalLetteredGrade = 'B';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
else
if((finalNumericGrade < 85) && (finalNumericGrade >= 78)) {
finalLetteredGrade = 'C';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
else
if((finalNumericGrade < 78) && (finalNumericGrade >= 70)) {
finalLetteredGrade = 'D';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
else
if((finalNumericGrade < 70) && (finalNumericGrade >= 0)) {
finalLetteredGrade = 'F';
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
}
break;
case 2:
cout << "Midterm Grade :";
cin >> response;
midtermGrade = atoi(response);
cout << "Final Exam Grade :";
cin >> response;
finalExamGrade = atoi(response);
cout << "Research Grade :";
cin >> response;
researchGrade = atoi(response);
finalNumericGrade = ((midtermGrade * SCIENCE_MIDTERM_PERCENTAGE) + (finalExamGrade* SCIENCE_FINALEXAM_PERCENTAGE)
+ (researchGrade * SCIENCE_RESEARCH_PERCENTAGE));
if((finalNumericGrade >= 90) &&(finalNumericGrade <= 100))
finalLetteredGrade = 'A';
else
if((finalNumericGrade < 90) &&(finalNumericGrade >= 80))
finalLetteredGrade = 'B';
else
if((finalNumericGrade < 80) &&(finalNumericGrade >= 70))
finalLetteredGrade = 'c';
else
if((finalNumericGrade < 70) &&(finalNumericGrade >= 60))
finalLetteredGrade = 'D';
else
if((finalNumericGrade < 60) && (finalNumericGrade >= 0))
finalLetteredGrade = 'F';
cout << endl << "**** SCIENCE STUDENT ****\n\n";
cout << "Midterm Grade : " << midtermGrade << endl;
cout << "Final Exam Grade : " << finalExamGrade << endl;
cout << "Research Grade : " << researchGrade << endl << endl;
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
break;
case 3:
cout << "Midterm Grade :";
cin >> response;
midtermGrade = atoi(response);
cout << "Final Exam Grade :";
cin >> response;
finalExamGrade = atoi(response);
finalNumericGrade = (midtermGrade * MATHS_MIDTERM_PERCENTAGE) + (finalExamGrade* MATHS_FINALEXAM_PERCENTAGE);
if((finalNumericGrade >= 90) & (finalNumericGrade <= 100))
finalLetteredGrade = 'A';
else
if((finalNumericGrade >= 83 ) & (finalNumericGrade < 90 ))
finalLetteredGrade = 'B';
else
if((finalNumericGrade >= 76 ) & (finalNumericGrade < 83 ))
finalLetteredGrade = 'C';
else
if((finalNumericGrade >= 65) & (finalNumericGrade < 76))
finalLetteredGrade = 'D';
else
if((finalNumericGrade < 65) & (finalNumericGrade >= 0))
finalLetteredGrade = 'F';
cout << endl << "**** MATHS STUDENT ****\n\n";
cout << "Midterm Grade : " << midtermGrade << endl;
cout << "Final Exam Grade : " << finalExamGrade << endl << endl;
cout << "Final numeric grade is : " << finalNumericGrade << "\n";
cout << "Final lettered grade is : " << finalLetteredGrade << "\n\n";
break;
default:
cout << "Is not a valid student type:)\n" << endl;
return 1;
}
cout << "Do you have more grades to calculate?\n";
cin >> moreGradesToCalculate;
cout << endl;
for(int x = 0; x < moreGradesToCalculate.length(); x++){
moreGradesToCalculate [x] = toupper(moreGradesToCalculate[x]);
}
}
cout << endl << "Thanks for using my grade calculator.";
return 0;
}
PLEASE HELP ME EXAMINE THE LOGICAL ERROR IN THIS CODE....
1. THE STRLEN() FUNCTION THAT CHECK IF THE USER PRESS THE ENTER KEY WITHOUT PROVING A RESPONSE TO THE QUESTION.
2. THE DEFAULT KEYWORD IN THE SWITCH STATEMENT THAT CHECKS A NON-INTEGER RESPONSE FROM THE USER.
|
|
|
|
|
Compiling does not mean your code is right!
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.
So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.
Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input Expected output Actual output
1 2 1
2 4 4
3 6 9
4 8 16 Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
{
return value * value;
}
Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Member 15281733 wrote: PLEASE HELP ME EXAMINE THE LOGICAL ERROR IN THIS CODE....
1. THE STRLEN() FUNCTION THAT CHECK IF THE USER PRESS THE ENTER KEY WITHOUT PROVING A RESPONSE TO THE QUESTION.
2. THE DEFAULT KEYWORD IN THE SWITCH STATEMENT THAT CHECKS A NON-INTEGER RESPONSE FROM THE USER. You've failed to explain exactly what the problem is. How are we, therefore, to know what your program is supposed to do?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
if((finalNumericGrade >= 83 ) & (finalNumericGrade < 90 ))
If you reach this statement the grade must be less than 90, because you already checked that in the previous statement. So all you need is
if(finalNumericGrade >= 83)
And similar for the following tests.
But putting all your code into main like that makes it so much more difficult to debug. Split the different parts out into functions to make it easier to see where your errors may be.
|
|
|
|
|
Yeah, that might go through a C++ compiler, but it's not really C++. I just didn't want to pile on.
OP - debugger is your friend.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
#include<iostream>
using namespace std;
int main()
{
const int b=14;
const float f=0.20;
cout<<"----------------"<<endl;
cout<<"BOOK LOAN SYSTEM"<<endl;
cout<<"----------------"<<endl;
cout<<"Enter the number of books : ";
int n;
cin>>n;
cout<<"\nEnter the days of the loan : ";
int l;
cin>>l;
cout<<"\n-------------------------------------"<<endl;
cout<<"Days overdue : ";
if(l>b){
cout<<l-b;
cout<<"\nFine : RM "<<(l-b)*(f)*n;
}
}
|
|
|
|
|
Do you mean in this snippet was used some other than C++ language? 
|
|
|
|
|
Did you mean to say "Help me convert this code I found on the internet to C so I can turn it in as my own work?"
|
|
|
|
|
it's already C++.
What are you wanting ? classes ?
I suggest using meaningful variable names instead of b or f or n.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
If you mean to replace std::cout and std::cin with stdio.h functions, then look up the usage of printf() and scanf(), e. g. at C Library - <stdio.h> - Tutorialspoint[^]
If you mean to convert everything to C, you also may need to remove the const qualifiers depending on the exact C compiler (and version) you are using.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
I'm trying to create an array of pointers to a common base class, but each pointer in the array will point to a different derived class instance. This is a 64-bit application.
Here's the critical portion of my code: (Unnecessary detail has been removed.)
Intellisense is warning me that the second pointer assignment (to the second element in the array) will cause a buffer overrun.
It says, "C6386: Buffer overrun while writing to 'this->m_Members': the writable size is 'this->m_MemberCount*8' bytes, but '16' bytes might be written."
I have never come across this warning before, and I wanted to ask the community if there is something obviously wrong with the code that I'm not seeing.
m_MemberCount = 7;
CGsUCharType* m_s_b1 = new CGsUCharType(); CGsUCharType* m_s_b2 = new CGsUCharType();
CGsUCharType* m_s_b3 = new CGsUCharType();
CGsUCharType* m_s_b4 = new CGsUCharType();
CGsUShortType* m_s_w1 = new CGsUShortType();
CGsUShortType* m_s_w2 = new CGsUShortType();
CGsULongType* m_S_addr = new CGsULongType();
CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
m_Members[0] = m_s_b1;
m_Members[1] = m_s_b2; m_Members[2] = m_s_b3;
m_Members[3] = m_s_b4;
m_Members[4] = m_s_w1;
m_Members[5] = m_s_w2;
m_Members[6] = m_S_addr;
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Your code should work (Intellisense is a little confused here), but may I suggest using std::vector instead:
#include <vector>
...
std::vector<CGsTypeBase*> m_Members(m_MemberCount);
This will ensure that your array is properly constructed and is destructed when it goes out of scope.
You may also wish to look into storing smart pointers (rather than raw pointers) in the vector. This can help ensure that they are destructed at the right time, as well.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Thank you for taking the time to respond. I will consider using vector instead.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
What are the definitions of CGsTypeBase and CGsUCharType ?
[edit]
I built this short program and it compiled and linked successfully:
struct CGsTypeBase
{
char foo[8];
};
struct CGsUCharType : CGsTypeBase
{
wchar_t foo[8];
};
struct CGsUShortType : CGsTypeBase
{
uint16_t foo[8];
};
struct CGsULongType : CGsTypeBase
{
long foo[8];
};
int main(
int argc,
char* argv[]
)
{
int m_MemberCount = 7;
CGsUCharType* m_s_b1 = new CGsUCharType(); CGsUCharType* m_s_b2 = new CGsUCharType();
CGsUCharType* m_s_b3 = new CGsUCharType();
CGsUCharType* m_s_b4 = new CGsUCharType();
CGsUShortType* m_s_w1 = new CGsUShortType();
CGsUShortType* m_s_w2 = new CGsUShortType();
CGsULongType* m_S_addr = new CGsULongType();
CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
m_Members[0] = m_s_b1;
m_Members[1] = m_s_b2; m_Members[2] = m_s_b3;
m_Members[3] = m_s_b4;
m_Members[4] = m_s_w1;
m_Members[5] = m_s_w2;
m_Members[6] = m_S_addr;
return 0;
}
So how does that differ from your code?
[/edit]
modified 6-Jul-21 3:54am.
|
|
|
|
|
Yes, my code compiles and links successfully too. I guess it's just intellisense that has a problem. Thank you for your response.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
A wild guess: the intellisense does not think (like, instead, you assume) that m_MemberCount is equal to 7 .
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
That's what I think as well. I'm glad to know someone else has the same idea. I did come across a few posts about that warning being incorrect.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: CGsTypeBase** m_Members = new CGsTypeBase*[m_MemberCount];
As others have pointed out the your solution works because of the way the memory lays down.
In the above you have a declaration of a variable which has a type. And you also give the variable a value. But the declaration of the variable is a pointer. Just a pointer. Not a pointer to an array. So when intellisense sees you use it as an array it goes basically 'pointer + 1' isn't going to work. It does works because of the value and not the declaration.
I believe there is a declaration form that allows you to specify the form that would make intellisense happy but been a long time since I attempted C++ so I will leave it to someone else to come up with the form that makes the array specific.
|
|
|
|
|
Thanks for your input. I appreciate you taking the time to post.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
We can put a "u" after a numeric literal to indicate that it should be compiled as an unsigned int.
Or we can put "ll" to indicate that it should be compiled as a long long.
But what is the correct character to indicate that it should be compiled as a short?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
There isn't one. You'd have to cast it: short(0) .
EDIT: I prefer to use int16_t or uint16_t rather than assuming that's what I'll get. But if you're working with code that already uses short , go ahead and use it.
|
|
|
|
|
Thanks, Greg.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Does anyone know if it's possible to debug a user application from a Windows service, or if there is something that prevents this?
I suppose I could find out by just trying it, but I'd rather not waste time creating and installing a skeleton service just to see that it's not possible.
I'm hoping someone might know one way or the other.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|