C++ Declaration error and how to solve it? [closed] - c++

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 2 days ago.
Improve this question
This is my code:
int main() {
cout << "Hello, world!" << endl;
return 0;
}
// preprocessor directive#include <iostream>using namespace std;
//Main functionint main()
{
string name;
cin >> name;
cout << "hi, " << name << endl;
cout << "End Program";
return 0;
}
and it says that the bracket below the
//Main functionint main() expects a declaration
I tried moving the bracket up and down, left and right

Related

Why do i have to reference in this situation? [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 1 year ago.
Improve this question
int main(){
while(true){
char input = getchar();
int x, y;
POINT xypos;
if (input == 'S' || input == 's'){
std::cout <<"enter new position" << std::endl;
std::cin >> x >> y;
SetCursorPos(x, y);
} else if (input == 'g' || input == 'G'){
GetCursorPos(&xypos);
std::cout << "X: " << xypos.x << "Y " << xypos.y << std::endl;
}
}
return 0;
}
Can someone please explain why with GetCursorPos, it has to reference the xypos object in the parameters? Why is it not possible to directly utilize it? Thanks
You probably mean why GetCursorPos(from the WinAPI) doesn't just return the position instead of taking a pointer and filling that right?
That's how the WinAPI works, almost all functions return BOOL to indicate success or failure and take information they populate by pointer.

I created a program to convert reals to dollars and the float variable is incorrect [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 3 years ago.
Improve this question
I created a program to convert real(the Brazilian coin) to the dollar and the coins are 4 digits and the float variable is more:
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[]){
float fReal;
cout << "enter the cash in real: R$:";
cin >> fReal;
float fDollar = fReal / 3.90;
cout << "your cash in dollar(s): " << fDollar << endl; // Here i want
//:.2f(from python) to the dollar value
cout << "Press enter to continue..." << endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
std::cout << std::setprecision(2)<< fDollar<<'\n';

c++ : How to ask a file name and print char Array [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 5 years ago.
Improve this question
i am new in c++, i am working on a project, in case 1 i need to ask a file name from a user and if the file name is correct to it should print something like 6x5 character Array. I am totally confused that how to start. any single help will be appreciated.
#include <iostream>
using namespace std;
int main(){
int x;
string file;
int temp;
cout << "Welcome to Urban Heat Island Model" << endl;
cout << "What would you like to do? " << endl;
cout << "1. Load land cover file " << endl;
cout << "2. Model Temperature Based on land cover" << endl;
cout << "3. Exit " << endl;
cin >> x;
switch (x){
case 1:
cout << "What is the name of the file to import? " << endl;
cin >> file;
break;
If there is a file that you want to open, you can try a file stream. Here is a good look at how to open a file for input and output. Just make sure to check if you can actually open the file before you try to read/write from/to it. You can do it with
if(inputFile.is_open())
{
//your code here
}
If you just want to see if it is a valid file extension, check the last part of the string. If you have a list of file names, compare what they input and what you have to see if they are the same.

How can I add numbers entered by the user using while or for loop in c++? [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 5 years ago.
Improve this question
It's really complicated to me as a beginner to do it so I tried this code :
int x,sum=0;
while (x)
cin >> x;
sum+=x;
cout << sum ;
I want to let the program when the user enters "0" the program should print the sum of these numbers entered by the user.
One way to do it is here:
#include <iostream>
int main()
{
int x;
int sum = 0;
std::cin >> x;
while(x)
{
sum += x;
std::cin >> x;
}
std::cout << "Sum: " << sum << std::endl;
return 0;
}
You are missing {}s around the statements in your while loop, so only cin >> x gets executed.

Whats the error in this code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void inputArray(double [], int );
void printArray(double [] ,int);
int main()
{
double rainfall[5];
rainfall[0]=1;
rainfall[1]=6;
rainfall[2]=9;
rainfall[3]=23;
rainfall[4]=67;
printArray(rainfall,5);
inputArray(rainfall,5);
}
void printArray(double array[],int size)
{
for(int i=0;i<size;i++){
cout<< "Rainfall is";
cout << array[i] <<endl;
}}
void inputArray(double array[], int size)
{
for(int i=0;i<size;i++){
cout << "Enter the Rainfall:";
cin >> array[i] << endl;
}
}
You can't do this:
cin >> foo << endl;
Near the last line of your code, it looks like you're trying to do something like:
Get some input and put it in array[i]
Echo the input and a new line?
You should do it like:
cin >> array[i];
cout << array[i] << endl;
Remember, cin >> foo means "take some input from the console and put it in foo," and cout << foo means "output foo to the console."
You can't cin "endl", you shoul cout it;
You asking user for entering values, but you don't use it.