void functions

void functions

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 20129 Views - Last Post: 15 May 2008 - 03:20 PM Rate Topic: -----

#1 Lydia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

void functions

Posted 06 May 2006 - 01:08 PM

I dont think I am getting my void function set up correctly?

#include <iostream>
#include <string>
using namespace std;

int main () {
string date;
int mark;

void displayrow (int mark =1; mark <=n; i++){
if for (mark >60; date < 2000;) ++i)
cout << "Congratulations! Extreamly good!" << endl;
else 
if (mark >60; date =< 2000)
cout << "Congratulations ! very good!" << endl;
}
else
if (mark =< 50; mark >= 60 && date < 2000){
cout << "Congratulations!" << endl;
}
else
if (mark > 50){
cout << "Unfortunaltely you got less than 50." << endl;
}

cout << "Enter the mark obtaind: ";
cin >> mark;
cout << "Enter the year you last wrote a comprehension test: ";
cin >> date;
//cout << "previous test before: " << date << endl;

mark<0;
while (mark!=0){
cout << "Enter the mark obtaind: ";
cin >> mark;
cout << "Enter the year you last wrote a comprehension test: ";
cin >> date;


return 0;
}


This post has been edited by Dark_Nexus: 06 May 2006 - 07:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: void functions

#2 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: void functions

Posted 06 May 2006 - 01:29 PM

You cannot have a function inside another function.

void foo()
{
//BODY
}

int main()
{
//BODY
}


Was This Post Helpful? 0
  • +
  • -

#3 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: void functions

Posted 06 May 2006 - 01:29 PM

ooooh my lord, syntax nightmare.

first off, you cannot place a function definition within another function (in this case, your 'void' function is within the main function).

Second, function syntax is as follows

returnDataType functionName(dataType parameter1, dataType parameter2, ...etc) { body of function }



Where returnDataType is the data type (int, string...etc, or void, in which case the function does not return a value), functionName is the name of the function, and your parameters are information required to run your function (note that parameters are optional). What you have in your function declaration looks kind of like a for loop.

So a void function that outputs a string passed to it would look kind of like this:

void outputString(char *strtoOutput)
{
cout << strtoOutput;
return; //note that a void function does not return a value
}



I'm not entirely sure what you're trying to do within the function; you frequently confuse looping syntax with if statements, etc.

If statements work as follows:

if (parameter1 COMPARISONOPERATOR parameter2)
{ // 'then'
   //do something here
}

//a functional if statement would look something like this
if (myString == theirString)
{
outputString(myString);
}

//also, semicolons are a no-no in if statements.  I think you might have //been trying to say 'AND' or 'OR'.  Here's how you would do that (AND = //&& and or = ||)
if (myString != theirString && myCounter > currentIndex || currentIndex == 0)
{
//do something
}



just try and follow these examples and clean up the code, you're just having syntax issues. If you need any help, just ask!
Out of curiosity, how many errors do you get when you try and compile that? :P
Was This Post Helpful? 0
  • +
  • -

#4 Lydia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: void functions

Posted 06 May 2006 - 01:40 PM

Oh my gosh you all make me laught :P
If I got a penny for every error I'm sure I would be able to buy atleast a new car. :P


I got your bit about using if statments and If statments are great to use

But The requirments where to use a void function. As for the rest of your responce sorry I have no idea what you were on about ( I have only been exposed to C++ in the last month or so and I am self taught so fare. Please go easy on me)

I had a list of marks and dates to input in the system untill 0 is entered.
That works beautifully!

But then....

after entering the mark and a year the following rules need to be applied

If the mark was above 60 and before year 2000
output will be Congrats Extream good

if the mark was above 60 and in year 2000 or later
outout will be congrats very good

if the mark was between 50 and 60 and the year was before 2000
out put will be congrats

and Lastly if the mark was below 50
output FAILED.


So yes if statements would be nice and easy (well sort off easy provided i sort out my errors) But I need to use a void function displaymessage to display the message most appopiot

hence my effort

void displayrow (int mark =1; mark <=n; i++){
if for (mark >60; date < 2000;) ++i)
cout << "Congratulations! Extreamly good!" << endl;
else 
if (mark >60; date =< 2000)
cout << "Congratulations ! very good!" << endl;
}
else
if (mark =< 50; mark >= 60 && date < 2000){
cout << "Congratulations!" << endl;
}
else
if (mark > 50){
cout << "Unfortunaltely you got less than 50." << endl;
}


This post has been edited by Dark_Nexus: 06 May 2006 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 msg555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 04-May 06

Re: void functions

Posted 06 May 2006 - 05:25 PM

I think you should invest in a good c++ book. There is just so much you need to understand that it can't possibly be done on these forums.
Was This Post Helpful? 0
  • +
  • -

#6 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4496
  • View blog
  • Posts: 28,404
  • Joined: 21-March 01

Re: void functions

Posted 06 May 2006 - 08:32 PM

msg555, on 6 May, 2006 - 04:17 PM, said:

I think you should invest in a good c++ book. There is just so much you need to understand that it can't possibly be done on these forums.

you're wrong.


Lydia, why don't we back up a bit and narrow the scope of the question, do you have a backup somewhere down the line of code that worked and compiled fine before you started adding extra code to it? Maybe we can start from there and we can start off doing function calls or logic in the body of your functions and work our way up from there.
Was This Post Helpful? 0
  • +
  • -

#7 msg555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 136
  • Joined: 04-May 06

Re: void functions

Posted 06 May 2006 - 08:56 PM

supersloth, on 6 May, 2006 - 07:24 PM, said:

msg555, on 6 May, 2006 - 04:17 PM, said:

I think you should invest in a good c++ book.  There is just so much you need to understand that it can't possibly be done on these forums.

you're wrong.

I didn't mean that this problem couldn't be solved here on the forums.

I meant that Lydia should try reading a book or some other material on c++ that presents the language in a clear and logical way. It will boost his/her ability to understand the language giving him/her a chance at solving his/her own problems.
Was This Post Helpful? 0
  • +
  • -

#8 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: void functions

Posted 06 May 2006 - 09:31 PM

msg555, on 6 May, 2006 - 07:48 PM, said:

supersloth, on 6 May, 2006 - 07:24 PM, said:

msg555, on 6 May, 2006 - 04:17 PM, said:

I think you should invest in a good c++ book.  There is just so much you need to understand that it can't possibly be done on these forums.

you're wrong.

I didn't mean that this problem couldn't be solved here on the forums.

I meant that Lydia should try reading a book or some other material on c++ that presents the language in a clear and logical way. It will boost his/her ability to understand the language giving him/her a chance at solving his/her own problems.

It is nice to know someone is trying to solve their own problems, but the entire purpose of this site is to help others LEARN, not to give answers, but rather to help others (including myself) to understand our mistakes and questions so that we can become better programmers :)

Quote

void displayrow (int mark =1; mark <=n; i++){

I am unsure as to whether this is supposed to be a for loop or a method declatation? Th code here is somewhere in the middle of the two
Was This Post Helpful? 0
  • +
  • -

#9 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: void functions

Posted 06 May 2006 - 09:44 PM

im with the msg guy on this one.. he/she should pick up a book and try to learn the language, and not expect us to teach her.

I know these forums are for helping people, but this going beyond helping. You guys are basicly teaching her C++. There are schools/colleges for this, forums should only help with specific questions. There is just so much wrong with her code, I dont think she even has the slightest idea of what she is doing. If she were to pick up a book and start learning im sure her questions will be much more educated and will have much less syntax errors.

I know when i was learning C++ and when i asked questions or posted code like this people just called me an idion, laughed, and told me to get a book and learn how to read. At the time i was pissed, but later i realized they were right.
Was This Post Helpful? 0
  • +
  • -

#10 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: void functions

Posted 06 May 2006 - 10:10 PM

fair enough, there are mistakes, but there is no use in discouraging someone who is showing effort, just misunderstanding their use and place.
*On the other side of that, it's exactly what happened to me, and i taught myself everything i know to date. You can learn more from trial and erroring your way through a tough problem.
I would suggest amazon.com and find a used C++ book, just about any will do, they will have examples and help with proper structure.

This post has been edited by William_Wilson: 06 May 2006 - 10:11 PM

Was This Post Helpful? 0
  • +
  • -

#11 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4496
  • View blog
  • Posts: 28,404
  • Joined: 21-March 01

Re: void functions

Posted 07 May 2006 - 04:35 AM

I may just be saying this cause it's 4 in the morning and I've had way too much to drink, but probably not, if you guys aren't interested in HELPING people, then maybe you should move on to somewhere that better suits your interests. I, and many others here, ARE here to help, so why don't you take your unwanted criticisms somewhere else and let us deal with the people who have questions.
Was This Post Helpful? 0
  • +
  • -

#12 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: void functions

Posted 07 May 2006 - 06:52 AM

I'm with msg555.
She should buy a book. Because that way she can learn stuff at her convenience and not when people reply to her posts everytime.
Sure </dic> is there to clear her concepts but as far as learning goes...it is possible to achieve it in a forum but as far as convenience goes...a Book suits best.

Book + </dic> + Interest to learn = Knowledge
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: void functions

Posted 07 May 2006 - 07:36 AM

Hi Lydia,

The elements you require for your function are all there...it is merely a matter of applying the proper syntax. While a book would be helpful in this instance, this forum is here to help people interested in learning the language, so your question is appropriate in my eyes.

The easiest way to write code is to write it in pseudocode first...structure your program in a sequential flow, then apply the proper syntax. You've esssentially written the pseudocode here:

Quote

f the mark was above 60 and before year 2000
output will be Congrats Extream good

if the mark was above 60 and in year 2000 or later
outout will be congrats very good

if the mark was between 50 and 60 and the year was before 2000
out put will be congrats

and Lastly if the mark was below 50
output FAILED.

If you wish to apply that logic in a function with no return type, there is no problem.

First, as noted, your function header needs to accept arguments (or not)...the syntax you currently have there
void displayrow (int mark =1; mark <=n; i++)


is incorrect...that syntax is generally associated with a for loop. For the purposes of this thread, I will assume that you wish to pass the mark to the function:
void displayrow (int mark, int date)


I am making the assumption that mark and date are not global variables.

In your function itself, you have also written your if statements with a for loop syntax...this is unlikely to achieve the results you desire. In general, it may be helpful to think of an if condition as something that will eveluate to true or false. For example, if you wish to test for the first condition you mentioned
if(mark > 60 && date < 2000)
cout << "Congratulations! Extreamly good!" << endl;


You can then expand to use an if else statement to test for each condition
if(mark > 60 && date < 2000)
cout << "Congratulations! Extreamly good!" << endl;
else if(mark > 60 && date >= 2000)
cout << "Congratulations! Very good!" << endl;


and so on, ending with an else default statement. One should also always keep error checking in mind to test for data integrity.

Overall, you may wish to check out a quick overview of control structures, such as the one here:
http://www.cplusplus...al/control.html
to familiarize yourself with the syntax. You'll pick it up very quickly, I'm sure! :)

Keep up the good work, and feel free to ask questions on the forum.
Was This Post Helpful? 0
  • +
  • -

#14 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: void functions

Posted 07 May 2006 - 10:30 AM

Well said Amadeus; I wish I had your clarity :).
Was This Post Helpful? 0
  • +
  • -

#15 Lydia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 03-May 06

Re: void functions

Posted 07 May 2006 - 01:00 PM

Ah thank you Videege for understanding. :genius:

its hard learning C++ with out additional help. :!:

Yes I do have aid books like C++ for dummies the all 7 in one collection item :sly:

But unless you point things out like you did with my void saying its looking like a for loop I would not have noticed. Although I will explain why I used a for loop.....

Thankx again.... :rolleyes:

sometimes I just need a hand and when I see the correct way as apose to what I was trying I see and understand more why mine would not have worked and I appreciate the basics more.. :wub:

FOR THE RECORD...THIS IS THE FIRST TIME I HAVE RELYED ON THE NET/FORUMS FOR HELP.....I HAVE LEARNT MORE IN MY FIRST 3 DAYS WITH YOU LOT THAN I HAVE IN MY FIRST 3 WEEKS EXPOSURE TO C++. FOR WHAT EVER ITS WORTH...I APPRECIATE IT! :cry2:

I have fixed my program and its run thew with no errors

#include <iostream>
#include <string>
using namespace std;

void displayRrow (int mark, int date ){
if (mark >60 && date < 2000 ){
cout << "Congratulations! Extreamly good!" << endl;
}
else if (mark >60 && date <= 2000){
cout << "Congratulations ! very good!" << endl;
}

else if (mark <= 50 && mark >= 60 && date < 2000){
cout << "Congratulations!" << endl;
}
else if (mark > 50){
cout << "Unfortunaltely you got less than 50." << endl;
}
}
int main (){
int mark, date;

cout << "Enter the mark obtaind: ";
cin >> mark;
cout << "Enter the year you last wrote a comprehension test: ";
cin >> date;

mark<0;
while (mark!=0){
cout << "Enter the  next mark obtaind: ";
cin >> mark;
cout << "Enter the year you last wrote a comprehension test: ";
cin >> date;
}
return 0;
}



But what I dont get (even reading threw my books) is how to recall your void messages. :angry:

Here is an example you can explain on (I got this from my study book)

void displayRow (int n){
for (int 1 = 1; i<=n; i++)
cout << '*';
cout << endl;
}
int main () {
int m;

cout << "Enter a positive integer: ";
cin >>m;

for (int j = 1; j <=m; j++)
displayRow (j);
return 0;
}


how does displayRow (j) know to actualy call displayRow (int n) and where did the j come from?

I suppose it would be better to follow the example in the book and to have my program as follows (note the for loop! (which is why originaly I got the two confused) but now I am getting an error `i' undeclared (first use this function)

void displayRrow (int mark, int date ){
for (mark >60; date < 2000; i++ ){
cout << "Congratulations! Extreamly good!" << endl;
}
else if (mark >60 && date <= 2000){
cout << "Congratulations ! very good!" << endl;
}

else if (mark <= 50 && mark >= 60 && date < 2000){
cout << "Congratulations!" << endl;
}
else if (mark > 50){
cout << "Unfortunaltely you got less than 50." << endl;
}
}
int main (){
int mark, date;

cout << "Enter the mark obtaind: ";
cin >> mark;
cout << "Enter the year you last wrote a comprehension test: ";
cin >> date;

mark<0;
while (mark!=0){
cout << "Enter the  next mark obtaind: ";
cin >> mark;
cout << "Enter the year you last wrote a comprehension test: ";
cin >> date;
}
return 0;
}




I know from the example above i would need another for loop at the end to recall my messages but I am still not clear on how to function it?
:blink:

This post has been edited by Dark_Nexus: 07 May 2006 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2