Jump to content

Recommended Posts

Posted

listen, i'm having trouble with a program i have to do for my computer science class. i'll post what i have so far. it' sin metroworks codewarrior. can someone help me out?

/* (name)

Period 6

RV 4-22

*/

#include <iostream.h>

int main()

{

 int counter;

 int high;

 int sum=0;

 

 cout<<"What is your max value?";

 cin>>high;

 cout<<endl;

 

 for(counter = 1;counter<=high;counter+2)

sum=sum+counter;

 

 cout<<"The sum of the odd numbers from 1 to "<<high<<" is "<<sum;

 return(0);

}

Posted

Ah, C++, my area. :)

The code seems to be fine. It's a very simple program, and it should work. Just one thing: why don't you declare the function main as void? There's no point in having it return 0, or anything else for that matter.

Posted

i'm practically just starting with C++. this is the only way i know how to do it so far. and aside from that, i hardly ever pay attention in class. the thing is tho, i keep getting an error on the for loop. any suggestions?

Posted

Lol, as always, the obvious is hard to see... I should have noticed this long ago. The for loop should be:

for (counter=1;counter<=high;counter=counter+2)

Posted

You're welcome. :)

When I get an error that I can't figure out, I usually just start doing random corrections to the faulty code until I get something that works. Or at least I find out what is NOT wrong.

Posted

A cobalt language I assume, to write programs? A more revisioned version I think. There is C, C+, and C++ (not an expert but this is what I learned).

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.