Help - Search - Members - Calendar
Full Version: Question Regarding C Programming
Forums > ABRSM > Forums Cafe
Oddball
OK, so I have this script:

CODE

#include <stdio.h>

int main()
{
   int n;
   printf("Enter a value:");
   scanf("%d", &n);
   if (n < 10)
      printf("This number is less than ten!!\n");
   return 0;
}


which I think is perfectly fine. Why is it when I compile it, it says that there is a multiple definition of 'main'??

Thanks in advance if you can help smile.gif
YetAnotherPianist
Odd, works fine for me.

Is that literally the only file that you're compiling? It sounds like it's a linker error - main is defined in another C file that has been compiled (into a .o file), and you're trying to join the two together to make the finished program.
Oddball
Sorry guys, solved it, it's getting late blink.gif

But if there are any C masters, how does one stop the program from ending before the user gives a response?? Is it that cin.get; thing?
Oddball
QUOTE(YetAnotherPianist @ Oct 8 2005, 10:52 PM)
Odd, works fine for me.

Is that literally the only file that you're compiling?  It sounds like it's a linker error - main is defined in another C file that has been compiled (into a .o file), and you're trying to join the two together to make the finished program.
*



Yeah I'm just not used to my software. Had other files open in the program that also were using int main()....
YetAnotherPianist
QUOTE(Oddball @ Oct 8 2005, 11:53 PM)
But if there are any C masters, how does one stop the program from ending before the user gives a response?? Is it that cin.get; thing?
*


I do mostly C++, but C's near enough.

What do you mean by 'ending before the user gives a response'? It waits for input at the moment; do you want it to wait for valid numerical input?
Oddball
Well when I run the program, I give it a number, say 5, and the program runs, I just get to see the output before it closes. How do I stop this and make the user press enter before the prog. closes?
mrbouffant
QUOTE(Oddball @ Oct 8 2005, 10:56 PM)
Well when I run the program, I give it a number, say 5, and the program runs, I just get to see the output before it closes. How do I stop this and make the user press enter before the prog. closes?
*



use getchar()...

or if you're on Windows™, just run your program in a command prompt and you'll see the output in the console window smile.gif
Oddball
Cheers Mr B. I'll have a look when I get home, am at school at the moment.

While we're here:

#include <stdio.h>

int main()
{
int n("How old are you?");
scanf("%d", &n);
if (n<= 10)
printf("You are less than ten!"\n");
if (n>= 11 | n<= 19)
printf("You are between eleven and twenty");
elseif {
printf("You are now considered old. Go and get your pension!!");
}
getchar();
return 0;
}

I have this. I'm a bit concerned about a few lines, mainly the one in red. Hope you can help!!! (Mrb, is this use of getchar() right?)
kmt63

whoops
YetAnotherPianist
Anyone who isn't aged between 11 and 19 inclusive will be told they are old, unless the if bit is restructured to something like:

CODE

if (n<= 10)
 printf("You are aged ten or younger."\n");
else if (n>= 11 | n<= 19)
 printf("You are between eleven and nineteen inclusive.");
else
 printf("You are now considered old. Go and get your pension!!");
}
kmt63
[quote=kmt63,Oct 10 2005, 01:16 PM][quote=Oddball,Oct 10 2005, 12:49 PM]Cheers Mr B. I'll have a look when I get home, am at school at the moment.

While we're here:

#include <stdio.h>

int main()
{
int n("How old are you?");
scanf("%d", &n);
if (n<= 10)
  printf("You are less than ten!"\n");
if (n>= 11 | n<= 19)
  printf("You are between eleven and twenty");
elseif {
  printf("You are now considered old. Go and get your pension!!");
}
  getchar();
  return 0;
}

I have this. I'm a bit concerned about a few lines, mainly the one in red. Hope you can help!!! (Mrb, is this use of getchar() right?)
*


Few things wrong here:

(n<=10) will return those less than or equal to 10 and not just less than.

(n>=11 | n<=19) | is a bitwise OR not a logical function use || in fact you probably want to use a logical AND so use && or AND.

You have a dangling elseif ... change it to else

Anyone over 19 isn't that old laugh.gif
Oddball
Cheers dudes wink.gif Will try these things when get home. Currently in ICT very very boreded.
YetAnotherPianist
QUOTE(Oddball @ Oct 10 2005, 02:30 PM)
Currently in ICT very very boreded.
*


I had that problem in ICT too - I just wanted to do programming, not have to play around with excel and make Access databases rolleyes.gif.
Oddball
We're acually doing access at the moment...and all I wanna do is compile this program...
AnotherPianist
QUOTE(Oddball @ Oct 10 2005, 02:33 PM)
We're acually doing access at the moment...and all I wanna do is compile this program...
*


Trust me, if you ever take it to degree level, your ability to program will be a lot more useful than access databases rolleyes.gif although you will have to suffer a small amount of that too (more the underlying structure of how they work than actually using them though)....
Oddball
They're just so annoying.....and my class. Been through the same thing. Made one database with tables, reports and forms in nearly two hours...

They're just so....GRRRRR!
YetAnotherPianist
QUOTE(Oddball @ Oct 10 2005, 02:42 PM)
They're just so annoying.....and my class. Been through the same thing. Made one database with tables, reports and forms in nearly two hours...

They're just so....GRRRRR!
*


I wonder, has your teacher told you to use the old trick of copy and pasting the SQL database definition out of Access into your coursework and claiming that's how you made the database for extra marks? wink.gif
mrbouffant
RANT ON!!

Access is the devil's work. According to a friend of a friend who knows about these things, Access was originally developed as a kind of "hobby" database for Mums to hold their recipes in etc.

What a shame legions of so-called experts have been trying to engineer n-tier business systems using it ever since ohmy.gif

(rant over, thank you)

Oddball
What a great idea! Might just do that.... ph34r.gif
YetAnotherPianist
Ahh, but MySQL is such a relief afterwards....

Then again, I think Windows was originally developed as a kind of "hobby" operating system and legions of so-called experts have been trying to engineer n-tier business systems using it ever since wink.gif.
mrbouffant
QUOTE(YetAnotherPianist @ Oct 10 2005, 01:46 PM)
Ahh, but MySQL is such a relief afterwards....

Then again, I think Windows was originally developed as a kind of "hobby" operating system and legions of so-called experts have been trying to engineer n-tier business systems using it ever since wink.gif.
*



MySQL: no triggers, stored procedures etc etc. nightmare to try and do anything serious in

Windows: you might be right, but I'm not biting the hand that feeds me (it pays for all those diploma entry fees tongue.gif)
YetAnotherPianist
QUOTE(mrbouffant @ Oct 10 2005, 02:49 PM)
MySQL: no triggers, stored procedures etc etc. nightmare to try and do anything serious in

True, never had to do anything serious yet though; if I did, out comes PostgreSQL...

QUOTE
Windows: you might be right, but I'm not biting the hand that feeds me (it pays for all those diploma entry fees  tongue.gif)
*


biggrin.gif.
kmt63
QUOTE(mrbouffant @ Oct 10 2005, 01:44 PM)
RANT ON!!

Access is the devil's work. According to a friend of a friend who knows about these things, Access was originally developed as a kind of "hobby" database for Mums to hold their recipes in etc.

What a shame legions of so-called experts have been trying to engineer n-tier business systems using it ever since ohmy.gif

(rant over, thank you)
*



LOL They used to say that about C.

Edwardo
QUOTE(mrbouffant @ Oct 10 2005, 01:44 PM)
RANT ON!!

Access is the devil's work. According to a friend of a friend who knows about these things, Access was originally developed as a kind of "hobby" database for Mums to hold their recipes in etc.

What a shame legions of so-called experts have been trying to engineer n-tier business systems using it ever since ohmy.gif

(rant over, thank you)
*



I've programmed in Access (no, it's not just a Mummy's database, but has a programming language attached) and Access 1.0 might have been rather noddy, but the best version (Access 97) was an extremely useful tool for creating apps for up to 20 or so users on a LAN. Provided you knew what you were doing, it worked very well, was highly stable, and the report builder was second to none. Systems that I wrote nearly ten years ago are still in use. Of course, it's not designed for n-tier systems - but only a fool would try to build such a system. Now I program in .NET and that's pretty good too, and SQL Server is my current DB of choice, but if I want quick and cheap, and not too many concurrent users, I'd go for Access every time. As for C - what a crock! I wrote avionics software for a few years in C - never again!
mrbouffant
QUOTE(Edwardo @ Oct 11 2005, 01:37 PM)
I'd go for Access every time.  As for C - what a crock!  I wrote avionics software for a few years in C - never again!
*



Let me guess, VB/VB.NET programmer?? lol tongue.gif
YetAnotherPianist
laugh.gif

Reminds me of one of my favourite Dilberts:

user posted image
SteveHopwood
QUOTE(AnotherPianist @ Oct 10 2005, 01:40 PM)
QUOTE(Oddball @ Oct 10 2005, 02:33 PM)
We're acually doing access at the moment...and all I wanna do is compile this program...
*


Trust me, if you ever take it to degree level, your ability to program will be a lot more useful than access databases rolleyes.gif although you will have to suffer a small amount of that too (more the underlying structure of how they work than actually using them though)....
*


Sad looser that I am, I just love programming user interfaces to Access databases in VB6.

Need to enter\edit data? Hah; here's a form to facilitate it.

Want data displayed in different ways? Hah; here's a SQL to do it.

Maybe I need to get a life.

Steve biggrin.gif
Edwardo
QUOTE(mrbouffant @ Oct 11 2005, 02:53 PM)
QUOTE(Edwardo @ Oct 11 2005, 01:37 PM)
I'd go for Access every time.  As for C - what a crock!  I wrote avionics software for a few years in C - never again!
*



Let me guess, VB/VB.NET programmer?? lol tongue.gif
*



Ding! Wrong!
mrbouffant
Doh! wink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.