What is wrong in this c++ code? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
Why does this code compile and work properly
#include <iostream>
int main()
{
using namespace std;
unsigned short int myInt = 99;
unsigned short int * pMark = 0;
cout << myInt << endl;
pMark = &myInt;
*pMark = 11;
cout << "*pMark:\t" << *pMark << "\nmyInt:\t" << myInt << endl;
return 0;
}
When this one doesn't compile:
#include<iostream>
using namespace std;
int addnumber(int *p, int *q){
cout << *p = 12 << endl;
cout << *q = 14 << endl;
}
int main()
{
int i , j;
cout << "enter the value of first number";
cin >> i;
cout << "enter the value of second number";
cin >> j;
addnumber(&i, &j);
cout << i << endl;
cout << j << endl;
}
In both the code snippets, I am assigning *pointer=somevalue. In the first code, it does not cause an error, but in the second, it causes error in the line
cout << *p = 12 << endl;
cout << *q = 14 << endl;
What mistake I am making?

Because of operator precedence.
To the compiler, your statement looks like
(cout << *p) = (12 << endl);

Related

Can't run this code Visual Studio (still work in another IDE) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed last month.
Improve this question
#include<iostream>
#include<vector>
using namespace std;
main() {
int x;
char str[80];
cout << "Enter a number and a string:\n";
cin >> x;
cin.getline(str, 80); //take a string
cout << "You have entered:\n";
cout << x << endl;
cout << str << endl;
}
It will show the error: There were build error https://imgur.com/jY8tYoA
I was try it on onlinegdb, it can run normally. I try create a new project in VS and put the code in but it still not worked
That's a small problem. In C++, we shoule always write int main().
#include<iostream>
#include<vector>
using namespace std;
int main() {
int x;
char str[80];
cout << "Enter a number and a string:\n";
cin >> x;
cin.getline(str, 80); //take a string
cout << "You have entered:\n";
cout << x << endl;
cout << str << endl;
}
This will solve the error.
And we can use string instead of char[].

Error in power of 5 of pow method in c++ [duplicate]

This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 5 years ago.
Whenever i write 5 as n and p as 2 ,i get the output as 24...please let me know what's wrong? for other numbers it is completely fine.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
double n, p;
cout << "enter the number" <<endl;
cin >> n;
cout << "enter the power" <<endl;
cin >> p;
int result = pow(n, p);
cout << "Result is " << result;
return 0;
}
You have problems with your data types!
double n, p; // here you use double types
std::cout << "enter the number" << std::endl;
std::cin >> n;
std::cout << "enter the power" << std::endl;
std::cin >> p;
int result = pow(n, p); // then here you use double pow(double, double) but assign it to an int
std::cout << "Result is " << std::result;
Solution:
double result = pow(n, p);

Alternative way to push into the stack [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I developed this program to add and the retrieve 15$ in the stack.
I was wondering is there another more efficient way to write my code.
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
const int MAX = 100;
int count;
stack<int, vector<int> > billStack;
for (int i=0; i<15; i++) {
billStack.push(i); // add 15 bills onto stack
}
cout << "The stack has " << billStack.size() << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
for (int i = 0; i< cash; i++) {
billStack.pop();
}
cout << "Cash out :" << cash << ". Remaining: " << billStack.size() << endl;
return 0;
}
Considering that you don't ever use the actual contents of the stack, just its size:
#include <iostream>
using namespace std;
int main()
{
int stackSize = 15;
cout << "The stack has " << stackSize << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
stackSize -= cash;
cout << "Cash out :" << cash << ". Remaining: " << stackSize << endl;
return 0;
}

Turning this code into a nested loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I am supposed to write a program, that will ask a user to input a number and then display all the perfect squares starting from 1 to the input number^2. I have written this program, but I was asked to put it in a nested loop. The problem is, I don't know how I could turn this into nested loop format. Does anyone have any ideas?
Here is my program so far:
int input;
int number = 1;
cout << "Enter a number: ";
cin >> input;
if(input > 1) {
cout << "The perfect squares are: ";
do {
cout << number*number;
number++;
input--;
if(input == 1) {
cout << " and "; }
else {cout << ", ";}
}while(input > 1);
cout << number*number << ".";
}
else if(input == 1) {
cout << "1";
}
else {
cout << "None.";
}
Why does it need to be a nested loop? This can be done using a simple for loop.
unsigned int input;
cout << "Enter a number: ";
cin >> input;
for (unsigned int i = 1; i <= input; ++i)
{
cout << i << " squared is " << i * i << endl;
}
Working demo
The while loop equivalent would be
unsigned int input;
cout << "Enter a number: ";
cin >> input;
unsigned int i = 1;
while(i <= input)
{
cout << i << " squared is " << i * i << endl;
++i;
}

How can I use a string from another function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my program ( I will include code below ), I have a function to determine the user's name and height. I use the name function first void name() and then the function void height() following it (of course main is last).
What I'm trying to do is to display the user's name throughout the program. In my second function, void height() Is ask the user how tall they are:
cout << " How tall are you?" << endl;
I would like to ask " How tall are you, name1?" , but the string name1 is not declared in the scope. Any ideas of how to make it work / what I'm doing wrong? Thank you. Also if you see any other issues or something I can do to make things easier/alternative ways, please let me know! (I'm new!)
#include <iostream>
#include <string>
using namespace std;
void name()
{
cout << "Welcome ________ ... uhmmmm, what was your name again? ";
string name1;
cin >> name1;
cout << " " << endl;
cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
}
void height()
{
//feet and inches to inches
cout << " How tall are you?" << name1 << endl;
cout << " " << endl;
cout << " " << endl;
cout << " Enter feet: ";
int feet;
cin >> feet;
cout << " " << endl;
cout << " Enter inches: ";
int inches;
cin >> inches;
int inchesheight;
inchesheight = (feet * 12) + inches;
cout << " " << endl;
cout << " Your height is equal to " << inchesheight << " inches total." << endl;
if (inchesheight < 65 )
{
cout << " You are shorter than the average male." << endl;
}
else if (inchesheight > 66 && inchesheight < 72)
{
cout << " You are of average height." << endl;
}
else
{
cout << " You are taller than average." << endl;
}
}
int main()
{
name();
height();
return 0;
}
Return a string instead of void.
string name()
{
cout << "Welcome ________ ... uhmmmm, what was your name again? ";
string name1;
cin >> name1;
cout << " " << endl;
cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
return name1;
}
Same thing with height(), for example, that should return an int. Also to get the name in your height function you could do.
int height(string name1)
{
// cout stuff about name
return userHeight;
}
Then you can call it like this:
int main()
{
string userName = name(); // takes the return from name and assigns to userName
int userHeight = height(userName); // passes that string into height()
return 0;
}
More examples of using functions and returning things:
int add(int a, int b)
{
int total = a + b; // the variable total only exists in here
return total;
}
int add4Numbers(int w, int x, int y, int z)
{
int firstTwo = add(w, x); // I am caling the add function
int secondTwo = add(y,z); // Calling it again, with different inputs
int allFour = add(firstTwo, secondTwo); // Calling it with new inputs
return allFour;
} // As soon as I leave this function, firstTwo, secondTwo, and allFour no longer exist
// but the answer allFour will be returned to whoever calls this function
int main()
{
int userA = 1;
int userB = 7;
int userC = 3;
int userD = 2;
int answer = add4Numbers( userA, userB, userC, userD ) // this grabs the value from allFour from inside the add4Numbers function and assigns it to my new variable answer
return answer; // now equals 13
}