Code crash on get method, 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 8 years ago.
Improve this question
I basically have a bool get function which returns true or false and sets the values.
This is my method...
bool Student::getMarks(int i, double *a, double *b)
{
{BOUNDS CHECKNG DONE HERE...
return false;}
THE LINE BELOW WORKS FINE AND PRINTS TO TERMINAL
//cout << marks[i][0] << "," << marks[i][1] << endl;
//THIS BIT CRASHES THE PROGRAM
*a = marks[i][0];
*b = marks[i][1];
return true;
}
This is my test program...
Test.cpp
double *a;
double *b;
Student student;
case 1:
student.getMarks(i, * a, *b);
cout << *a << endl;
cout << *b << endl;
break;
default:
break;
If a mark exists it returns true and sets the mark to *a and *b.
However, the program crashes.
Would greatly appreciate it if someone could tell me where I am going wrong. I want it so that the test program actually returns both *a and *b

You want this:
double a;
double b;
Student student;
case 1:
student.getMarks(i, &a, &b);
cout << a << endl;
cout << b << endl;
break;

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.

value is always set to 1 even if the input is 0 [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
ok so i writ this little code in c++. keep in mind i just started learning c++ so this problem confused me a lot
when i run this code in code blocks (
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer = 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
the answer value is always set to one even if i type 0
i tried coding another if statement for the answer value if it was 0 but that didn't work either
In C++, = operator is an assignment operator and it sets value of left operand to value of right operand. Therefore the value of Answer becomes 1 thanks to Answer = 1.
You should use == operator to check equality.
Use the == operator in your code, as = operators does the job of assigning values.
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer == 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)

Returning values from a separate function in C++? [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 am doing a programming assignment in my CS class using C++ that is giving me problems. The assignment is to write a program that asks the user to input a Fahrenheit temperature. The program must then output the Celsius equivalent of that temperature and the Celsius equivalents of the next 20 Fahrenheit temperatures. This must all be done using a separate function to complete the conversion calculation as well as a function prototype.
I got everything to work except my Celsius outputs all show a value of 0. I cannot seem to find any errors or figure out what is wrong...Please Help!
#include <iostream>
#include <iomanip>
using namespace std;
double Celsius(double temp);
int main() {
double fTemp, cTemp;
cout << "Please input a temperature in degrees Fahrenheit: ";
cin >> fTemp;
cout << "F Temp" << " " << "C Temp"<< endl;
cout << "-------------------" << endl;
for(int count=1; count<= 20; count++){
cTemp = Celsius(fTemp);
cout << setw(6) << fTemp << setw(12) << cTemp<< endl;
fTemp++;
}
return 0;
}
//*************Celsius Function****************//
double Celsius(double temp){
double newC;
newC = (temp-32) * (5/9);
return newC;
}
//************************************************
(5/9) is integer-integer division. You might've as well typed 0 there. Replace it with (5.0/9.0) and it should be fine.
One could simply try (5.0/9.0) or use a type cast by doing something like
double Celsius(double temp){
double newC;
newC = (temp-32) * (float)5/9;
return newC;
}
Thereby, you will not be getting a straight 0 in your outputs.

How to write and implement your own function [closed]

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 need to write my own sqrt function: double my_sqrt_1(double n)
How would I go about doing this? At first I tried putting this outside of "int main()":
double my_sqrt_1(double n)
{
int x = 1;
x = (x + n / x) / 2;
}
I then put this:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << x;
}
I also tried:
int main()
{
cout << "Please enter a value ";
cin >> my_sqrt_1;
cout << '\n' << my_sqrt_1;
}
None of this worked though. I'm probably doing this completely wrong, but it made sense in my head.
"I'm probably doing this completely wrong ..."
Sorry to say that, but yes.
You need a variable to receive input, and call your function passing that variable
int main() {
cout << "Please enter a value ";
double myNumber;
cin >> myNumber;
cout << '\n' << my_sqrt1(myNumber) << endl;
}
Also your function is supposed to return the result of the calculation
double my_sqrt_1(double n) {
double x = 1.0;
// ^^^^^^ ^^
x = (x + n / x) / 2.0;
// ^^
return x; // <<<<<<<<<<<<<<
}

C++ char arrays -- Why the garbage? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am trying to get only the alphabetic characters from an array of characters entered by the user. Here is a snippet:
const int SIZE(100);
int main()
{
char* entryTextArray = new char[SIZE];
char* adjustedTextArray= new char[SIZE];
int i, j;
cout << "Enter text, and I will tell you if it is a palindrome!" << endl;
cin.get(entryTextArray, SIZE);
cout << "Length of char array is " << strlen(entryTextArray) << endl;
for(i=0, j=0; i <= (strlen(entryTextArray)); i++) {
if(isalpha(entryTextArray[i]) && (entryTextArray[i] != '\0')) {
adjustedTextArray[j] = entryTextArray[i];
cout << adjustedTextArray[j] << endl;
j++;
}
}
cout << adjustedTextArray << endl;
}
When I compile, the cout of the adjustedTextArray displays the proper individual entrys, but the cout outside of the loop is the entry text, followed by garbage. I have no idea what is wrong! Help?!
You have the condition:
if (something && (entryTextArray[i] != '\0'))
so you are explicitly avoiding to copy the NUL terminating value from entryTextArray to adjustedTextArray. So you need to place it manually.
But since you are working in C++ using std::string just makes more sense.