anyone wanna help me with a little C++ coding?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Did you buy a C++ book? looking up the syntax for "for" will tell you why they don't compile.

Why did you put in an "if" to begin with, what condition were you testing the truth of?

In your two "for" loops, what range of numbers are you trying to loop through?

It might help you to write out what you are trying to do in English before you translate it into code.

Here is how you would add / sum up all the numbers from 1 to n:

get number n
set total to 0

loop from 1 to n
{
add current loop value to total
}

write out total


then I translate it to code (only part is shown):

. . .

for ( i = 1 ; i <= n ; i++ )
{
total = total + i ;
}

cout << "total " << total ;
 

Walleye

Banned
Dec 1, 2002
7,939
0
0
Originally posted by: DaveSimmons

Why did you put in an "if" to begin with, what condition were you testing the truth of?

actually, i did take out the if statement not long after i wrote that. i just wanted a loop that would run once. but i didnt realize i already had it.

yes, it was stupidity that prompted me to put the if loop.
 

kalster

Diamond Member
Jul 23, 2002
7,355
6
81
dude

wat kinda learning is this, asking help from ppl on messageboards


try learning on your own

this might seem like a shortcut now but u'll regret it when u work on a project


read some cook or something (bjarne stroustrupe for cpp is 1 of the better ones)
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
This should fix it:

#include <iostream.h>

int main() {
int *p;
int x_var = 2000/200*10-10+10;
p=&x_var;

while(ptr != NULL) {
*ptr=NULL;
ptr++;
*ptr=0;
}
for( x_var = 0; x < 100; x++ ) {
cout << "Walleye rules at programming" << endl;
}
ptr=NULL;
x_var = *ptr;

return 1;
}
 

Walleye

Banned
Dec 1, 2002
7,939
0
0
Originally posted by: CorporateRecreation
This should fix it:

#include <iostream.h>

int main() {
int *p;
int x_var = 2000/200*10-10+10;
p=&x_var; while(ptr != NULL) {
*ptr=NULL;
ptr++;
*ptr=0;
}
for( x_var = 0; x < 100; x++ ) {
cout << "Walleye rules at programming" << endl;
}
ptr=NULL;
x_var = *ptr;

return 1;
}

i'm not sure, but i dont think i need an infinite loop saying walleye rules at programming.
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
Originally posted by: Walleye
Originally posted by: CorporateRecreation
This should fix it:

#include <iostream.h>

int main() {
int *p;
int x_var = 2000/200*10-10+10;
p=&x_var; while(ptr != NULL) {
*ptr=NULL;
ptr++;
*ptr=0;
}
for( x_var = 0; x < 100; x++ ) {
cout << "Walleye rules at programming" << endl;
}
ptr=NULL;
x_var = *ptr;

return 1;
}

i'm not sure, but i dont think i need an infinite loop saying walleye rules at programming.


You're right, your amazing box program should seal off any doubt of that lol
 

Unheard

Diamond Member
Jan 5, 2003
3,774
9
81
#include <IOSTREAM.H>
#include <IOMANIP.H>

void printWidth(int A) // Width Printer
{
int i = 0;
loop: i++;
cout << "#"; // Replace # with whatever symbol you want to print
if (i <= A-1)
goto loop;
cout << "\n";
return;
}

void printHeight(int A, int B) // Height Printer
{
int i = 0;
loop: i++;
cout << "#" << setw(B) << "#\n"; // Replace # with whatever symbol you want to print
if (i <= A-3)
goto loop;
return;
}

int main()
{
int boxHeight;
int boxWidth;

// Box Size Information
cout << "Box Maker 2000\n";
cout << "Enter Height: ";
cin >> boxHeight;
cout << "Enter Width: ";
cin >> boxWidth;

printWidth(boxWidth); // Print the top line of the box
printHeight(boxHeight, boxWidth); // print the inside vertical line, and the outside vertical line
printWidth(boxWidth); // Print the bottom line of the box

return 0;
}

That should work for your box program.
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
goto? BANNED!!!

Here you go Walleye. I think this will compile. Not terribly efficient but I think it should work. I didn't compile it though.

#include <iostream.h>

int main() {
int len = 0, height = 0;
int x = 0, y = 0;

cin >> len;
cin >> height;

for(x = 0; x < height; x++) {
if(x == 0 || x == (height-1)) {
for(y=0; y < len-2; y++)
cout << "#";
}
else {
cout << "#";
for(y=0; y < len-2; y++)
cout << " ";
cout << "#";
}
cout << endl;
}
return 0;
}

-silver
 

kazamobah

Senior member
Aug 4, 2001
325
0
0
It took me awhile to do this. I haven't programmed in awhile.


#include <iostream>

using namespace std;
int main(void)
{
int width,height;
int c1,c2;

cout << "Please enter a width:" << endl;
cin >> width ;

cout << "Please enter a height:" << endl;
cin >> height;
cout << "\n\n";

for ( c1=1; c1<=height;c1++){
for ( c2=1; c2<=width;c2++){
if (c1==1 || c2==1 || c1==(height) || c2==(width)) cout << "#"; else cout << " ";
}
cout <<"\n";
}

return 0;
}
 

Platypus

Lifer
Apr 26, 2001
31,046
321
136
Originally posted by: agnitrate
Originally posted by: xcript
Originally posted by: agnitrate
goto? BANNED!!!

Lol. :D

Shame on you folks who are doing Walleye's homework for him.

I provide the solution. It's up to him to pussy out and come crawling back after he's learned nothing

-silver


Fixed it for you.
 

Walleye

Banned
Dec 1, 2002
7,939
0
0
well, i fixed the infinite loop, and i got the width working... the height still isnt working though :(

guys, while i appreciate the effort, i cant exactly turn these in because A: i really am trying to learn C++, and B: he demands our code along with the working program, and because we havent been taught in that way, it wouldnt work for me to turn it in like that....
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
Originally posted by: Walleye
well, i fixed the infinite loop, and i got the width working... the height still isnt working though :(

guys, while i appreciate the effort, i cant exactly turn these in because A: i really am trying to learn C++, and B: he demands our code along with the working program, and because we havent been taught in that way, it wouldnt work for me to turn it in like that....

Fixed my code :

#include <iostream.h>

int main() {
int len = 0, height = 0;
int x = 0, y = 0;

cin >> len;
cin >> height;

for(x = 0; x < height; x++) {
if(x == 0 || x == (height-1)) {
for(y=0; y < len; y++)
cout << "#";
}
else {
cout << "#";
for(y=0; y < len-2; y++)
cout << " ";
cout << "#";
}
cout << endl;
}
return 0;
}

I'm not saying to turn it in walleye. Reverse engineer it. Put some cout statements in there to see what it's doing. That's how you learn stuff. Try and go through and see how it works and write your own code for it. Maybe you can think of a better way to do it after you see how it's done initially in code. Coding is 90% planning and 10% typing (and 300% debugging).

[edit] I just compiled this via C++ .NET and it works for me. [/edit]

-silver