-3

I know it is duplicated... I didn't understand any of the other threads. Literally just started to learn programming. Currently trying to learn C++ as my first language. Ran into this error, I did google it but I didn't really know what they were talking about. I looked at both of my "int main" things and they are exactly the same. No errors. I guess formatted wrong. Here is the code. I'm currently playing around with the std::count and variables, along with std:cin

#include <iostream>

int main()
{
    std::cout << "Hello, how are you doing? I suppose you are only here to read this. Oh well.";
    return 0;
}

int main()
{
    std::cout << "Please feed me a number: " << "It can be any number."; // Asking human to enter a number.
    int x{ }; // get number from keyboard and store it in value x
    std::cin >> x; // recieved number and is now entering console
    std::cout << "Thank you for feeding me " << x << '\n';
    return 0;
}
datagen
  • 9
  • 2
  • How do I properly embed my code into the thread? I see that it is very clungy right now. – datagen Mar 01 '19 at 09:48
  • 1
    Nice one :) You're only allowed to have **one** main function. – YSC Mar 01 '19 at 09:49
  • What is the error? You can't put 2 main functions in a single file. – Alan Birtles Mar 01 '19 at 09:50
  • the error is int main(void)' already has a body : note: see previous definition of 'main' btw, what do I replace with main then? – datagen Mar 01 '19 at 09:51
  • If you want to create a new program, you have to create a *new* program, source files and all. You can't have multiple programs in a single "program". More specifically, you can only define a symbol (like `main`) *once* in a single program. – Some programmer dude Mar 01 '19 at 09:52
  • 4
    Don't try to learn `c++` by guessing and experimenting. Get your hands on a good book or tutorial to ger you started. – super Mar 01 '19 at 09:54
  • Oh no, this is all meant to show up in one program. I want to publish the program and see what it looks like, but it gives me this error. – datagen Mar 01 '19 at 09:54
  • 2
    please fix your title, the title should be a brief summary of the problem/question. And btw you dont want help from unexperienced people?, kinda discriminatory :P – 463035818_is_not_an_ai Mar 01 '19 at 09:54
  • 3
    When learning C++, you might want to follow a [good book](https://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO Mar 01 '19 at 09:55
  • "I know it is duplicated... " when you already found duplicates then you should explain why they didnt help and what exactly is missing for you to understand, otherwise you will just get the same answers ...or get flagged as duplicate – 463035818_is_not_an_ai Mar 01 '19 at 10:04

1 Answers1

-1

A C++ program need only one main() function. This latter is called at program startup. Your code should look like this :

#include <iostream>

 int main()
 {
  std::cout << "Hello, how are you doing? I suppose you are only here to read this. Oh well." << std::endl;

  std::cout << "Please feed me a number: " << " It can be any number." << std::endl; // Asking human to enter a number.
  int x{ }; // get number from keyboard and store it in value x
  std::cin >> x; // recieved number and is now entering console
  std::cout << "Thank you for feeding me " << x << std::endl;
  return 0;
 }

Here a link for more reading about the main() function.

asendjasni
  • 963
  • 1
  • 16
  • 36
  • Alright, thank you. I removed an int main and a return 0 along with a "{" Let's see if it works. – datagen Mar 01 '19 at 09:58
  • 1
    It worked. It looks very sloppy though and all of the words are together. Will have to work on that. Haha, would be interesting to write a book in one program using this language. – datagen Mar 01 '19 at 09:59
  • As suggested in the comment above, try to change the question so it describes the problem. Now you can accept the answer. – asendjasni Mar 01 '19 at 10:01
  • @datagen If this answers your question, click the green tick on the left to mark your problem as solved. – asendjasni Mar 03 '19 at 19:37