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 5 years ago.
Improve this question
Fill up the blanks with appropriate keyword to get the desired output according to the test cases. (Language use c++)
Sample Test Cases
input output
Test Case 1 4 square = 16, ++ square = 25
test case 2 -8 square =64, ++ square = 49
#include <iostream>
using namespace std;
______ int SQUARE(int x) { ______ x * x; }
int main() {
int a , b, c;
cin >> a ;
b = SQUARE(a);
cout << "Square = " << b << ", ";
c = SQUARE(++a);
cout << "++ Square = " << c ;
return 0;
}
Second blank place is "return"
First may be "inline" or nothing
Related
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 1 year ago.
Improve this question
[this is my code with problem. I have already installed mingw with vscode on my laptop
. how do I solve the identifier 'd' undefined in C++\C . It takes me to the edit include path in problem console after i click on quick fix.I don't know what should I do? please explain in simple language as Iam new to coding ?]
code
[i have posted the 2 screenshot of my code][
#include<iostream>
using namespace std;
int main(void)
{
int a = 4 ;
int b = 5 ;
float pi = 3.14 ;
char c ='d';
cout<< "hello world. \n Here the final value of a is "<<a<<". \n The value of b is "<< b;
cout<< "\n the value of pi is "<<pi;
cout<<"\n the value of c is : " <<d;
return 0 ;
}
]
You never declared the variable 'd' anywhere. That's a typo. It should have been 'c'.
#include<iostream>
using namespace std;
int main(void)
{
int a = 4 ;
int b = 5 ;
float pi = 3.14 ;
char c ='d';
cout<< "hello world. \n Here the final value of a is "<<a<<". \n The value of b is "<< b;
cout<< "\n the value of pi is "<<pi;
cout<<"\n the value of c is : " <<c;
return 0 ;
}
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 3 years ago.
Improve this question
I'm trying to extract 3 numbers out of the semantic version string which has the following format: "v%d.%d.%d"
Here's my example code:
std::string myVersion = "v3.49.1";
int versionMajor, versionMinor, versionPatch;
getVersionInfo(myVersion, versionMajor, versionMinor, versionPatch);
std::cout << versionMajor << " " << versionMinor << " " << versionPatch << '\n';
The expected result:
3 49 1
How can I design the function getVersionInfo()?
What would be the most elegant solution?
Use sscanf:
void getVersionInfo(std::string version, int &major, int &minor, int &patch) {
sscanf(version.c_str(), "v%d.%d.%d", &major, &minor, &patch);
}
Try it here
Maybe You can try this
#include <iostream>
using namespace std;
int main() {
string s="v3.49.1";
int major,minor,patch;
int x=s.find(".");
//We dont want v symbol so start from 1 till 1st dot
major=stoi(s.substr(1,x));
cout<<major;
//Then find second dot from first symbol and copy as sub string
int y=s.find(".",x+1);
minor=stoi(s.substr(x+1,y-x));
cout<<minor;
//After that remaining string will be patch version
patch=stoi(s.substr(y+1));
cout<<patch;
return 0;
}
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 3 years ago.
Improve this question
I try to build a simple C++ program. A interactive calculator, console based. I have a problem with the while loop. It does the code inside the body but not in a specific order to look ok on console.
I tried to place the sum = sum + num outside the while but is wrong. I tried many things and this doesn't work.
int add , sum = 0 ;
while(add==1 && num!=0){
cout << "Enter a number , when you are done please type in 0 : ";
cin >> num;
numbersEntered++;
if (num!=0){
//the problem kind of begins here
sum = sum + num;
cout<<" Your total is : " <<sum<<endl;
The output is like:
Enter a number , when you are done please type in 0 : 5
Your total is : 5
Enter a number , when you are done please type in 0 : 4
Your total is : 9.
I type 5 and 4 in this example. To be clear i don't expect this output.
Assuming you want to print the sum only at the end, this should work. Just take it out of the while loop. It will stop only when a 0 is entered.
#include <iostream>
using namespace std;
int main() {
int add = 1, sum = 0, numbersEntered = 0, num = 1;
while(add==1 && num!=0) {
cout << "Enter a number, when you are done please type in 0: ";
cin >> num;
numbersEntered++;
sum = sum + num;
}
cout << "Your total is: " << sum << endl;
}
The if condition to check if num is not 0 can be removed since sum + 0 is the same as not adding to sum.
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 7 years ago.
Improve this question
#include "stdafx.h"
#include <iostream>
int sqr ( int x ) //sqr means square of x
{
return x * x ;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "enter ur number";
int x ;
std::cin >> sqr (x) ;
std::cout << "square of ur number is : " << sqr ;
return 0 ;
}
I just started with C++ 2 days back and while trying doing some problems I am now stuck. Please tell me what's the problem, and make the answer understandable because I'm new to the language and might not understand certain terms.
You can only read into a variable:
std::cin >> x;
but you can output the value of any (suitably typed) expression:
std::cout << sqr(x);
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 8 years ago.
Improve this question
int main()
{
long long x,y,z,result;
char f,g;
cin >>x>>y>>z;
**result** =
cout << result ;
return 0;
}
How to make result = x (+ or - or / or *) y (+ or - or / or *) z !?
Reading the operators in between the numbers is simple:
long long x,y,z;
char f,g;
cin >>x>>f>>y>>g>>z;
// See what you've got
cout << x << " " << f << endl;
cout << y << " " << g << endl;
cout << z endl;
However, figuring out the result of the operation is trickier: you need to check the values you've got in f and g, and perform the operations as needed. Note that there must be no space between your numbers and the operators, otherwise the input would be processed incorrectly.
Demo.
This is probably at the core of the exercise that you are solving, so I will suggest that you write a function like this:
long long compute(long long a, long long b, char op) {
... // Check the operator, and return the result
}
With this function in hand, you can produce the result in one simple call:
long long result = compute(compute(x, y, f), z, g);
Once you write the compute function, this should give the result that you expect.
You can do cin>>astring. And separate the string by delimiter and convert them to integer.
For example:
1,2,3
will become '1','2','3'.