"Exception thrown: Write access violation in C++" [closed] - c++

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 2 days ago.
Improve this question
I get this error when I try to compile the following code: Exception thrown: write access violation.
this was 0xF81EFA0000.
#include <iostream>
using namespace std;
class Demo
{
int x;
public:
void setX(int i)
{
x = i;
}
int getX()
{
return x;
}
};
int main() {
Demo obj[4];
int i;
for (i = 0; 1 < 4; i++)
{
obj[i].setX(i);
}
for (i = 0; i < 4; i++)
{
cout << "obj[" << i << "].getX(): " <<
obj[i].getX() << endl;
}
// Textual Data Types
// Boolean
return 0;
}
The error concerns line 10, where "x = i" is written. Any help would be good.
I haven't tried anything to fix it, other than writing brackets after "x", which obviously didn't work. I expect this program to print an array of objects when working properly.

Related

Is it possible in C++ to type multiple lines of string outputs without have to continually add the prefix "cout"? [closed]

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 2 years ago.
Improve this question
I just started learning C++. I have seen examples of coders typing code like this...
int main() {
cout << "hello";
<< "world";
return 0;
}
but when I tried that it seemed to me that I have to write it like this...
int main() {
cout << "hello";
cout << "world";
return 0;
}
How can I do this like the original example shows?
You might mean:
int main() {
cout << "hello" // <- no trailing `;`
<< "world";
return 0;
}
That will print:
helloworld
If you wanted then on separated rows, you can write:
int main() {
cout << "hello\n"
<< "world\n";
return 0;
}
That will produce:
hello
world
But, there is another, less known, feature of the compiler - concatenation
of raw strings:
int main() {
cout << "hello"
"world";
return 0;
}
That will also work. This too gives helloworld as output.

Creating cpp multiplication table using for loop [closed]

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 2 years ago.
Improve this question
output should be like this I hope u guys can help me..
Here's the code that I tried:
int main() {
int x,y;
for (x=1; x<=10; x++) {
cout << x << "|";
for (y=1; y<=10; y++) {
cout << x*y << "\t";
}
cout << endl;
return 0;
}
}
Your program is terminating just after the first iteration of the loop, the reason behind this is that you misplaced return 0 inside the loop instead of putting it at the end, so your program is returning after first iteration of the loop.
here is the fix
int main()
{
int x,y;
for (x=1; x<=10; x++)
{
cout<<x <<"|";
for (y=1; y<=10; y++)
{
cout<<x*y<<"\t";
}
cout<<endl;
}
return 0;
}

C++ invalid use of nonstatic data member [closed]

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
I have looked around and have sen this question asked a fair bit but I the problems they are having seem different to mine.
I am only a beginner so I find it kind of hard to understand whats wrong with my program. Here is the code:
#include <string>
#include <iostream>
using namespace std;
class Character
{
int health;
string action;
public:
void setHealth(int hp) {health = hp;}
void setAction(string act) {action = act;}
int getHealth() {return health;}
string getAction() {return action;}
};
int main()
{
int difficulty;
Character player;
player.setHealth(15);
Character enemy;
cout << "What difficulty would you like to play? easy = 1, medium = 2, hard = 3 ";
cin >> difficulty;
switch (difficulty)
{
case 1 : enemy.setHealth(10); break;
case 2 : enemy.setHealth(15); break;
case 3 : enemy.setHealth(20); break;
}
cout << "\nEnemy health = " << enemy.getHealth << endl;
return 0;
}
And here is the error message I get: In function 'int main()':
36:39: error: invalid use of non-static member function
It appears the problem is at the cout at the bottom of the main function.
Please help!
getHealth() is a class method, not a member, so it should be:
cout << "\nEnemy health = " << enemy.getHealth() << endl;

why am I getting random results when incrementing an int [closed]

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 8 years ago.
Improve this question
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.

Trying to convert a string to double under conditions [closed]

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
I have a function which uses an std string parameter to test if there is an alpha character. It would be best to test for only numeric characters but I have not gotten that far just yet. I just am trying to get it to recognize if the input does not have a number in it. If not it loops the error message until there is only numbers. After this I am trying to convert the string to double by use of atof() so it can be returned in main(). I get a debug assertion failed! Message upon runtime which says, expression string subscript out of range if a number is put in. Other wise if a letter has input, it keeps looping its self with the error message. I got my code for the function below. Any one have any clues as to what I am doing wrong? I am out of ideas...
double Bet::betProb(std::string b)
{
bool alphChar = false;
double doubleBet;
for(int i = 0; i < b.size(); i++){
if(isalpha(b[i])){
alphChar = true;
}
}
while(alphChar){
cout << "Error! Bet only with numbers." << endl;
cin >> b;
for(int i = 0; i < b.size(); i++){
if(!isalpha(b[i])){
alphChar = false;
}
}
}
string F=b;
int T=F.size();
char Change[100];
for (int a=0;a<=T;a++)
{
Change[a]=F[a];
}
doubleBet = atof(Change);
return doubleBet;
}
Since your problem has already been resolved, I thought I'd show you the way this would be done using standard C++ functionality:
#include <string>
#include <stdexcept>
double Bet::betProb(const std::string& str)
{
double d;
try {
d = std::stod(str);
} catch (const std::invalid_argument&) {
std::cerr << "Argument is invalid\n";
throw;
} catch (const std::out_of_range&) {
std::cerr << "Argument is out of range for a double\n";
throw;
}
return d;
}