new to c++, ran into a problem

hey
I'm new to c++ and i've done a few tutorials, but i find i learn better by experimenting with stuff, so im trying to add a few commands(from a website i googled) onto the stuff i did in the tutorial, but one of the lines from the tutorial doesn't work now. if someones able to help me, that would be great. its something really simple probably, and if someone wants to tell me whats wrong ill copy/paste it all in here or whatever.

thanks
i'll take a look at it :)
I and probably many other members of this forum will be more than willing to help you out if you paste the code along with any errors your compiler may have spitted out.

-Albatross
alright, thanks, theres probably a bunch of stuff wrong in this and such, but i was just messing around, and anyway,

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>
#include <iostream>

using namespace std;

int main()
{
printf("HEY MACHITA, WHATS UP?\n");
cout<< "what NiNe ThOuSaNd??\n";
printf("ha ha ha \nit took forever to get it to make new lines");
cout<< " but i... \nFIGURED IT OUT";
cout<< "\nand now, something completely different";
}

int number()
{
cout<< "Please enter a number: ";
cin>> number;
cin.ignore();
cout<<"You entered this number: "<< number <<"\n";
if (number==a) {cout<< "\nthats my lucky number!";}

cin.get ();
}


thats not all of it, i got rid of some of it and im going to try putting it in differently. im trying to make this in a way that depending on the number entered, a different message is shown, but the line "cin>> number;" isnt working as it did in the tutorial. if im doing everything wrong, tell me, and ill take another look at the tutorials again...
a and number do not appear to be defined as variables. In fact, a doesn't even appear to be defined...

EDIT: Details:
When I compiled this, my compiler spat these errors:
a is not declared in this scope
Ambiguous overload for operator>> in std::cin >> number

In short, you need to declare a and number as variables, and rename your function to something else.

-Albatross
Last edited on
i got rid of those lines before i copied/pasted, but i had

int a;
a = 7;

(or something similar) in there, after

"int number()"

i believe
What about your number variable?

And if you declared int a after your defined your int number() then that won't work. I'm not 100% sure what you mean, but if you declared a after you defined your function, that's not going to work. You have to declare things before they are used.

-Albatross
oh, what are you using to compile? it seems more useful than what im using, mine just highlights the line, but yours seems to do more.
this is pretty much what it looked like with the variables,

...
int number()
int a;
a = 7;
{
cout<< "Please enter a number: ";
cin>> number;
...
what i saw as problem is that main needs a return type like 0 also the function number
and you have declared number as function and also as variable and use it as test !!
confusing the compiler wether it's a call to function or variable
and try to sperate the \n from the text ur outprinting
thanks, i took out part of it, and it worked, also, could you tell me how i would write something in c++? i want to state something like,

if x=1 do something, if not, skip
if x=2(etc.)

this should be all i need, i hope
I'm using GCC, but it has a front-end, known as XCode. Virtually all sane compilers would give the error... what are you using as an IDE and what are you using as a compiler?

Also, regarding the variable, I don't think you can do that. Try placing the variables before int number().

EDIT: For using if statements, else statements, and so much more: http://cplusplus.com/doc/tutorial/control/

@zinogg
Most sane some compilers will spit a warning if there is no return listed for main(), but will not require it. At least, gcc does...

-Albatross
Last edited on
well, im using one program, bloodshed dev-C++, i heard about it in a tutorial and got it, not sure what an IDE is...
Thanx Albatross,i am using VC++ who requires a return type but only void functions doesn't

warning C4508: 'main' : function should return a value; 'void' return type assumed
cpp(26) : error C4716: 'number' : must return a value
Hmm. Okay, good to know.

And IDE is short for Integrated Development Environment. It's where you input your code. Unless I'm mistaken, Dev C++ is both an IDE and a compiler...

-Albatross
thanks for the link albatross, and dev c++ is an IDE and a compiler like you said
another question, i tried googleing this but got nothing, is there a command of some sort that jumps back and re-reads a line X number of times? thanks in advnce
Topic archived. No new replies allowed.