I need help writing a program for class online now.

// Assignment 4B.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

#include <iostream>
#include <string>
using namespace std;
using std::cout;
using std::cin;

int n ()
{
cout<<"How many Xs?"<<endl;
cin>>n;
int c;
c=0;
while (c<n)
{
cout <<"x";
c++;
}


I'm getting 2 error messages. 1 - IntelliSense: no operator ">>" matches these operands 2 - IntelliSense: operand types are incompatible ("int" and "int (*)()")
closed account (3qX21hU5)
There are a number of things that aren't right here, but don't worry just starting out it is very hard to get the proper syntax and format down. I'll go down the list on what I noticed.

1)
1
2
#include <iostream>
#include <string> 


These are #include statements. They tell you program that you are going to be using code from different header files on your system. I won't go into the details right now because that is a bit farther down the road for you but you will learn it later.

What I will say is that these ALWAYS should be at the top of your files. In your case these should be right after #include "stdafx.h" and before int main().

2) Same thing with your using declarations.

1
2
3
using namespace std;
using std::cout;
using std::cin;


You need to have these declared before anything that uses them. So they should come right after your #include's and before int main(). These I also won't go into detail on but you will learn more about them later.

3) To simplify things you can use just int main() instead of having int _tmain(int argc, _TCHAR* argv[]) (Which is not right I don't think).

Anyways int main() is where you program starts. It is the first thing that is called in most programs so think of it as the heart of your program. Everything that you want to run gets called in main().

Now notice that you don't have anything in main except return 0;. Basically right now your program does nothing, it just opens up and then closes again because there is nothing inside main().

4) There was a bunch of things wrong with what you had after int main() so I just edited your code to how I believe you wanted it to look and added some comments for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>  // You don't need this either since you don't use a string
using namespace std;
// using std::cout;  You didn't need both of these because of using namespace std;
// using std::cin;   Which include both of these.

int main()
{

    /* I moved everything inside of main since this is where it should be
     * and is where I believe that you wanted it. Wasn't sure what you were
     * doing before but this is how you want it.
     */

    int n;    // You forgot to put a ; after int n.

    cout << "How many Xs?" << endl;
    cin >> n;

    int c = 0; // This is how to initialize a variable when you declare it.

    while (c<n)
    {
        cout <<"x";
        c++;
    }
    return 0;
}


Hope this helps a bit and also next time please use codetags (Hint: The <> off to your right, just highlight all your code that you pasted and click that button) it makes it so much easier to read your code on the forums.

Good luck with your programming and keep with it. It gets a easier and much more fun after you get through the basics.
First, pleas always use code tags - edit your post, then select your code then press the <> button on the right under format menu.
It should look like the code below

Your program does nothing - it starts with main then returns 0. main should call your function.

When posting compiler errors always post them in full.

It also needs reorganising.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//#includes  all of them here

//using statements
using namespace std; //no need for this
using std::cout;
using std::cin;
using std::endl; //make sure you have all of them

//otherwise put std:: before each std thing

//function declarations
int n();  //tells the compiler that this function exists, so there is no problem when it is called in main()

//the main function
int _tmain(int argc, _TCHAR* argv[]) {

n(); //call the function

return 0;
}

//function definitions
int n() {

//your code here
}


Hope this helps - get back to us if there are more problems.


EDIT: Ninja'd by Zereo, we were almost saying the same things :)

I will learn to type faster on day .......
Last edited on
closed account (3qX21hU5)
Mwhaha
Ok so i kinda figured something out so now I'm doing a "do" "while" problem and the problem asked for 100 asterisks. i got it to be numbered from 0-100 but how do i change that to asterisks?? this is what i have::

// practice 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}

#include <iostream>
using namespace std;
int main ()
{
char n=50;
int c=0;
do {
cout <<c<<endl;
c=c+1;
}
while (c<=n);
}
Last edited on
Are you going to take on board the advice given to you already? And you cannot have 2 main functions.
yes i will. i just need help im behind in my class but the more i work on this the more i understand it. all of you have been really helpful. and i thank all of you seriously. can you please edit it for me so i see how it should be and then i can get a better understanding??
closed account (3qX21hU5)
We gave you everything you need even for the second problem you have. All you need to do is change a few things that should be easy for you if you are working on loops. Also again please use code tags
Last edited on
Topic archived. No new replies allowed.