#### Archived

This topic is now archived and is closed to further replies.

# C++ help!

This topic is 5639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I can''t think right now of a simple algorithm... can someone help me? I want a user to input a number, then this program will do a simple calculation where it adds the square from 1 to the number. ie... if user enters, 5 it should, do, (1*1) + (2*2) + (3*3) + (4*4) + (5*5) I started off with a user input and then a function. in the function, there is a for loop... for(counter=number;counter<0;counter--) but then, I dunno what to do after this.

##### Share on other sites
(define f  (lambda (n)    (if (= n 1)      1      (+ (f (- n 1)) (* n n)))))(define g  (lambda (n k)    (if (= n 1)        (k 1)        (g (- n 1) (lambda (m)                     (k (+ m (* n n))))))))

Take your pick, and yes I am bored.

##### Share on other sites
ehm..

long getVal(int n){  long result = 0;  for(int i = 1; i <= n; i++)    result += i*i;  return result;}

##### Share on other sites
thx for the help guys

##### Share on other sites
*cough homework cough*

##### Share on other sites
k, this was actually what I was working on from a book, and not homework...
**Write a program that accepts an integer and adds the square of all integers from 1 to the number entered.**

so far... I got...

#include <iostream>
using namespace std;

void SquareTotal(int toSquare);
int toSquare;
int main()
{
cout<<"this program will square from 1 to the number you have entered.\n"< cout<<"please enter a number: ";
cin>>toSquare;
if(toSquare <=0)
{
cout<<"please enter a non negative, non zero number."< cout<<"number: ";
cin>>toSquare;
}
cout<<"the number you have entered is: "<
SquareTotal(toSquare);

cout<<"the total is: "<
return 0;
}

void SquareTotal(int toSquare)
{
int total=0;
for (int counter =1;counter {
total = counter*counter;
}
}

but it doesn''t add from 1^2+2^2+3^2+...+n^2 and so forth.
can someone help me?

##### Share on other sites
You are neglecting to do anything meaningful with your function. You call a function with no return value and no alternate way to communicate its findings.

You should set the return type to an int, return total, assign the return value of the function to some variable in your main function, and output that variable. It''s simple really.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

##### Share on other sites
thanks for the help,, but it still doesn''t do what it should.

I changed the code to...
#include <iostream>
using namespace std;
int SquareTotal(int toSquare);

int main()
{
int toSquare;
cout<<"this program will square from 1 to the number you have entered.\n"< cout<<"please enter a number: ";
cin>>toSquare;
if(toSquare <=0)
{
cout<<"please enter a non negative, non zero number."< cout<<"number: ";
cin>>toSquare;
}
cout<<"the number you have entered is: "<
SquareTotal(toSquare);

cout<<"the total is: "<
return 0;
}

int SquareTotal(int toSquare)
{
int total=0;
for (int counter =1;counter<=toSquare;counter++)
{
total +=counter*counter;
}
}

but the output is still...

this program will square from 1 to the number you have entered.

the number you have entered is: 5
the total is: 5
Press any key to continue

##### Share on other sites
Uhm.. what's this code?
cout<<"the number you have entered is: "< SquareTotal(toSquare);cout<<"the total is: "<return 0;
You don't print anything returned? Or does your code end up malformed by the forum? In that case you should use code or source tags when posting code.

PS. "a non negative, non zero number" -> "a positive number"?

[edited by - Jedyte on June 4, 2003 6:23:52 AM]

[edited by - Jedyte on June 4, 2003 6:24:36 AM]

1. 1
2. 2
Rutin
15
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633269
• Total Posts
3011153
• ### Who's Online (See full list)

There are no registered users currently online

×