6G Celicas Forums

Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
> Anyone know C++?, Code Inside...getting weird errors
post Oct 12, 2006 - 2:04 AM
+Quote Post
Coomer



Administrator
*****
Joined Aug 23, '02
From Seattle, WA
Currently Offline

Reputation: 14 (100%)




I've been writing a program for class and was getting strange errors,
so I went through everything and eventually simplified it down to this super-simple program:

CODE
#include <iostream>
using std::cout;
using std::cin;

int main() {
    int buckets[9][4] = {0};

    for (int j = 0; j <= 9; j++) {
        for (int k = 0; k <= 4; k++) {
            cout << buckets[j][k] << " ";
        }
        cout << "\n";
    }

    char ch;
    cin >> ch;
}


Sounds simple, right? Create a 10 row, 5 column 2-dimensional array filled with zeroes, right?

The problem is that my output is this:



Anyone have any idea as to what could be going on? If I manually fill the array with zeroes using two for loops, I get the correct output, but when the program tries to exit after I enter a character, I get this error:



Any of you programmers out there have any ideas?
Attached image(s)
Attached Image Attached Image
 


--------------------
New Toyota project coming soon...
post Oct 12, 2006 - 3:21 AM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




when I put that in and ran the program I got essentially the same output (with some different numbers at the end) but I didn't get an error message when I exited the program. confused.gif

heres exactly what I put in...
IPB Image

edit: I'm terrible at this stuff but I thought I'd give it a try anyway

This post has been edited by hurley97: Oct 12, 2006 - 3:22 AM


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 12, 2006 - 6:32 AM
+Quote Post
JonCars17



Enthusiast
**
Joined Dec 6, '05
From South Carolina
Currently Offline

Reputation: 0 (0%)




I asked a net friend of mine and this is what he said

QUOTE
Friend:
well I'm not too sure, and for some reason my compiler isn't working .-_-
but it looks like his array is screwed up
he buts buckets[9][4] wanting an array that's 10x5 array but you have to be literal when you make an array
he need to create the array with the actual size, bucket[10][5] then use it like he trying to within the code, bucket[0-9][0-4]

post Oct 12, 2006 - 8:37 AM
+Quote Post
phonex98



Enthusiast
**
Joined Mar 25, '06
Currently Offline

Reputation: 0 (0%)




I agree with Jon. Its been a while since ive done c++.... maybe 4 years tongue.gif But i think if you call the array with [9][4] Your maximum value that you put into it needs to be [8][3]. Oh, and what is using std::cout; ?


--------------------
----- '94 ST hatch --- Yellow -----
post Oct 12, 2006 - 8:55 AM
+Quote Post
Rjb23



Enthusiast
*****
Joined Feb 8, '04
From KY
Currently Offline

Reputation: 0 (0%)




QUOTE(JonCars17 @ Oct 12, 2006 - 11:32 AM) [snapback]490649[/snapback]

I asked a net friend of mine and this is what he said

QUOTE
Friend:
well I'm not too sure, and for some reason my compiler isn't working .-_-
but it looks like his array is screwed up
he buts buckets[9][4] wanting an array that's 10x5 array but you have to be literal when you make an array
he need to create the array with the actual size, bucket[10][5] then use it like he trying to within the code, bucket[0-9][0-4]



This is correct. You can see by the random numbers its pulling out of the array but if that doesn't work run your code in debug and step through and look at the variables especially the array and see what it looks like(check and make sure it is really 10 x 5).

CODE

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    int buckets[10][5] = {0};

    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 5; k++) {
            Console::Write(buckets[j][k] + " ");
        }
    Console::Write("\n");
    }
    return 0;
}


What are you doing with the cin, and ch variable?

Coomer if you need more help pm me I'm a developer I mostly do javascript, sql, vb and asp.net. But C++ was the first language I learned I may be rusty but I can help.

This post has been edited by Rjb23: Oct 12, 2006 - 9:09 AM
post Oct 12, 2006 - 9:38 AM
+Quote Post
phonex98



Enthusiast
**
Joined Mar 25, '06
Currently Offline

Reputation: 0 (0%)




im pretty sure he's using the cin (input) as a sort of 'pause' so his MS-dos windows will remain open until he puts some sort of input in. Otherwise as soon as he ran the program, the milliseconds it takes to generate an array of 0's would happen, and the window would close before the user had a chance to see if the program infact did what he wanted.


--------------------
----- '94 ST hatch --- Yellow -----
post Oct 12, 2006 - 12:05 PM
+Quote Post
Starcraftjunkie

Enthusiast
**
Joined Nov 21, '05
From Wpg, MB. Canada
Currently Offline

Reputation: 3 (100%)




x2. Initalize with the [10][5] and it should be fine.

http://www.cplusplus.com/doc/tutorial/arrays.html

QUOTE(phonex98 @ Oct 12, 2006 - 8:37 AM) [snapback]490668[/snapback]

I agree with Jon. Its been a while since ive done c++.... maybe 4 years tongue.gif But i think if you call the array with [9][4] Your maximum value that you put into it needs to be [8][3]. Oh, and what is using std::cout; ?

post Oct 12, 2006 - 12:17 PM
+Quote Post
Coomer



Administrator
*****
Joined Aug 23, '02
From Seattle, WA
Currently Offline

Reputation: 14 (100%)




Awesome...that worked. Thanks a ton guys. smile.gif


--------------------
New Toyota project coming soon...
post Oct 12, 2006 - 3:21 PM
+Quote Post
tomazws



Enthusiast
*****
Joined Oct 30, '04
From So Cal
Currently Offline

Reputation: 13 (100%)




wow... haven't seen a C++ code for quite a while... cin, cout... classic!


--------------------
post Oct 12, 2006 - 3:34 PM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




speaking of which... I should be writing a program about a boat crossing a river right now. I don't know how you guys can understand this stuff. I get some of it but I don't get why some things are used... kindasad.gif

ex. what is buckets? is that another way to declare variables like "float" or "double"? cause thats what I normally put where "int buckets" is

This post has been edited by hurley97: Oct 12, 2006 - 3:37 PM


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 12, 2006 - 8:27 PM
+Quote Post
Starcraftjunkie

Enthusiast
**
Joined Nov 21, '05
From Wpg, MB. Canada
Currently Offline

Reputation: 3 (100%)




Coomer is using buckets as a variable name. He can call it whatever he wants (within reason). It is just a variable to store some value, and in this case, it is storing integer values.
post Oct 12, 2006 - 10:49 PM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




ahh. the use of "int" confused me, I just use "short" and "long". I believe long is the same as int when used to define variables.

I'm starting to understand... but still overall confused kindasad.gif

I'm working on this right now: "Write a program that calculates the distance, Ddown, given the river width, Wriver, the rowing velocity, Vrow, and the water flow velocity, Vwater."


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 12, 2006 - 11:14 PM
+Quote Post
XS4lv1Truch0x

Enthusiast
****
Joined Dec 9, '05
From Long Beach
Currently Offline

Reputation: 1 (100%)




lmao i thought visual basic wass bad now im lookin at this its a mean ass headache.

i wana learn this!!!

lol but i suck


--------------------
Is this good enuff 4 ya? :D

IPB Image
post Oct 12, 2006 - 11:33 PM
+Quote Post
Steevo



Enthusiast
*
Joined Jul 19, '06
From Colorado
Currently Offline

Reputation: 0 (0%)




< Pulls out hair <_<



I prefer Delphi as it is so much easier, but when I get into code I can read it like a book. But I just hate it.


I was kicked out of class in school when I coded a checksum error and check that would lock and consume all the resources on a PC in a couple seconds. This was in the late 90's though. I was gonna make a script to run it on boot, and to distribute it to the 9X registry through allowed shares.

The teacher didn't think that it was funny. And thus ended my schooling in C++.
post Oct 12, 2006 - 11:40 PM
+Quote Post
XS4lv1Truch0x

Enthusiast
****
Joined Dec 9, '05
From Long Beach
Currently Offline

Reputation: 1 (100%)




QUOTE(Steevo @ Oct 12, 2006 - 9:33 PM) [snapback]491046[/snapback]

< Pulls out hair <_<



I prefer Delphi as it is so much easier, but when I get into code I can read it like a book. But I just hate it.


I was kicked out of class in school when I coded a checksum error and check that would lock and consume all the resources on a PC in a couple seconds. This was in the late 90's though. I was gonna make a script to run it on boot, and to distribute it to the 9X registry through allowed shares.

The teacher didn't think that it was funny. And thus ended my schooling in C++.


lmao yup hackers are always kids lmao!

i remember my computer teacher, he had a program that didnt let us get internet til he enabled it, lol i wrote a visualbasic code and i got internet and everyone else wass like oh damn how u do that lmao

i dun think it wass visual basic. ohhh now that i thnk of it it was more of a .bat file.


--------------------
Is this good enuff 4 ya? :D

IPB Image
post Oct 13, 2006 - 12:08 AM
+Quote Post
phonex98



Enthusiast
**
Joined Mar 25, '06
Currently Offline

Reputation: 0 (0%)




QUOTE(hurley97 @ Oct 12, 2006 - 8:49 PM) [snapback]491032[/snapback]

ahh. the use of "int" confused me, I just use "short" and "long". I believe long is the same as int when used to define variables.

I'm starting to understand... but still overall confused kindasad.gif

I'm working on this right now: "Write a program that calculates the distance, Ddown, given the river width, Wriver, the rowing velocity, Vrow, and the water flow velocity, Vwater."


Ive actually got a physics midterm tomororw so i cant spend too much time helping you... But if you need help with that program its actually a very simple program.

Input Wriver
Input Vrow
Input Vwater
Ddown = (Time to cross [Vrow/Width])*Vwater

Or something like that... oh how i miss programming and simple physics tongue.gif

And i'd have to bust out my old c++ text but im pretty sure long and int are not that same..


--------------------
----- '94 ST hatch --- Yellow -----
post Oct 13, 2006 - 12:48 AM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




yeah I got that far (not sure if it'll work yet). its the end where you tell it to say 'you survive' if you dont go over the waterfall or 'you die' if you go over that I am stuck on.

heres what I have so far, I know there are things wrong:

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

int main ()
float ddown, wriver, vwater, vrow, dwf;

cout << endl << "Enter value of width of the river";
cin >> ddown;

ddown=(wriver*(vwater/vrow))

do
{
if ;
done='y';
} while (done='y');
cout << endl << "Congrats! You Survive.";
{
if ;
done='x';
} while (done='x');
cout << endl << "Ouch! You have drowned.";

return 0;
}


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 13, 2006 - 1:11 AM
+Quote Post
Starcraftjunkie

Enthusiast
**
Joined Nov 21, '05
From Wpg, MB. Canada
Currently Offline

Reputation: 3 (100%)




err. Hurley, your program doesn't make any sense.. from your first post of the problem, this is what I get from it..

Given the width (Wriver), rowing speed (Vrow), and river speed (Vwater), find Ddown, which is the distance you have travelled down from your original location?

Where is this waterfall and death coming from. biggrin.gif is that part 2?
post Oct 13, 2006 - 1:15 AM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




QUOTE(Starcraftjunkie @ Oct 13, 2006 - 2:11 AM) [snapback]491084[/snapback]

err. Hurley, your program doesn't make any sense.. from your first post of the problem, this is what I get from it..

Given the width (Wriver), rowing speed (Vrow), and river speed (Vwater), find Ddown, which is the distance you have travelled down from your original location?

Where is this waterfall and death coming from. biggrin.gif is that part 2?

lol. yeah theres appeantly a waterfall that you cant go over. like I said, I am confused. heres the instructions for the last part:
"You can then print out some results. For example
• "Print out" (cout) the values of Ddown and Dwf.
• If you did not go over the waterfalls, print out
a congratulations such as "you survive".
• If you go over the waterfalls, print out a regrets
message such as "You hurt.""


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 13, 2006 - 1:17 AM
+Quote Post
Starcraftjunkie

Enthusiast
**
Joined Nov 21, '05
From Wpg, MB. Canada
Currently Offline

Reputation: 3 (100%)




o ya.. make sure you have a line to check the input of Vrow. tongue.gif If it's 0, you should make sure it stops the program and screams an error message or else your program will crash and burn when it divides by 0. wink.gif

ahhh.. I get it.

So after you calculate Ddown (which you've done right).
You compare it against the value of Dwf (distance to waterfall).

If Ddown is bigger, you've fallen.. otherwise, you're safe.
post Oct 13, 2006 - 1:18 AM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




QUOTE(Starcraftjunkie @ Oct 13, 2006 - 2:17 AM) [snapback]491086[/snapback]

o ya.. make sure you have a line to check the input of Vrow. tongue.gif If it's 0, you should make sure it stops the program and screams an error message or else your program will crash and burn when it divides by 0. wink.gif

ahhh.. I get it.

So after you calculate Ddown (which you've done right).
You compare it against the value of Dwf (distance to waterfall).

If Ddown is bigger, you've fallen.. otherwise, you're safe.

correct. my problem is expressing it in C++ terms


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 13, 2006 - 7:56 AM
+Quote Post
Starcraftjunkie

Enthusiast
**
Joined Nov 21, '05
From Wpg, MB. Canada
Currently Offline

Reputation: 3 (100%)




ez. Just use an IF loop.

you calculated your Ddown. (checking for stupid user input, such as a negative or a zero)

you know Dwf (from asking it earlier with a Cin)

IF (Ddown < Dwf)
// you assume you fall even if Ddown = Dwf

{ blah blah, you're safe }

ELSE

{ blah blah, you didn't paddle fast enough, foo, and now lie at the bottom of the waterfall with a bunch of rocks }

and why is your program returning an INT? Shouldn't you just declare it with a VOID it so you don't have to return a value at the end?
post Oct 13, 2006 - 8:13 AM
+Quote Post
Rjb23



Enthusiast
*****
Joined Feb 8, '04
From KY
Currently Offline

Reputation: 0 (0%)




What is a if loop and why would you need a loop?

All you have to do is after your calculations are made is this,

if (Ddown<Dwf)
{
cout << "You fell over the waterfalls, you are dead RIP!";
}
else
{
cout << "You made it to the shore, you dodged that one you must have 9 lives!";
}

This post has been edited by Rjb23: Oct 13, 2006 - 8:13 AM
post Oct 13, 2006 - 9:42 AM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




31 errors

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

int main ()
int ddown, wriver, vwater, vrow, dwf;

cout << endl << "Enter the width of the river";
cin >> wriver;
cout << endl << "What is the rowing velocity?";
cin >> vrow;
cout << endl << "What is the water flow velocity?";
cin >> vwater;

ddown=(wriver*(vwater/vrow))
dwf=50

do
{
if ddown<dwf;
{
cout << endl << "Congrats! You Survive.";
}
else;
{
cout << endl << "Ouch! You have drowned.";
}
return 0;
}


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 13, 2006 - 11:40 AM
+Quote Post
tomazws



Enthusiast
*****
Joined Oct 30, '04
From So Cal
Currently Offline

Reputation: 13 (100%)




CODE

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

int main () {
   double ddown, wriver, vwater, vrow;
   double dwf = 50.0;
   cout << endl << "Enter the width of the river";
   cin >> wriver;
   cout << endl << "What is the rowing velocity?";
   cin >> vrow;
   cout << endl << "What is the water flow velocity?";
   cin >> vwater;

   ddown = wriver * (vwater / vrow);

   if (ddown < dwf) {
      cout << endl << "Congrats! You Survive.";
   } else {
      cout << endl << "Ouch! You have drowned.";
   }
}


I haven't touched C++ for 10 years, I've been a Java/PHP/VB(ASP) guy since then. I hope this code would work.


--------------------
post Oct 13, 2006 - 2:00 PM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




hey thanks! that helped A LOT! biggrin.gif

now hopefully I don't mess it up doing the other parts... rolleyes.gif


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06
post Oct 14, 2006 - 12:19 AM
+Quote Post
Starcraftjunkie

Enthusiast
**
Joined Nov 21, '05
From Wpg, MB. Canada
Currently Offline

Reputation: 3 (100%)




Is the Dwf provided by the question? or do you have to ask the user for the info?
post Oct 14, 2006 - 7:33 PM
+Quote Post
hurley97



Enthusiast
*****
Joined Mar 3, '04
From Portsmouth, RI
Currently Offline

Reputation: 33 (100%)




it wasn't provided but it didn't say to ask the user to enter it so I just made it up. I think I have to make the distance bigger cause I keep dying kindasad.gif


--------------------
7A-FTE: It's not about the money. Our Beams Swap.

I <3 Dustin---07/16/06

2 Pages V   1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: July 27th, 2025 - 9:51 AM