Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Passing data between functions?
New Posts  All Forums:Forum Nav:

C++ Passing data between functions? - Page 3

post #21 of 36
Thread Starter 
Quote:
Originally Posted by Tomiger View Post
Code:
if( ! (prices[k] > 0)
   k = -1;
else
   continue
To restart the loop, you essentially need to reset the counter variable, k. If you get it to -1 when that condition is true, the loop will then increment k as it always does, and then k will be 0.

I'll look over the other code now. So what exactly was being printed when you say it was garbage? I don't have the input files, so I can't test it directly.
If you want to use the input iv attached it. A very simple file. This way you can test the program appropriately. What it was printing out was stuff like 0ex000000 and stuff like that. I honestly cant remember exactly what it was printing out. It was nothing that helped me and i tried working with it for a few hours.
post #22 of 36
Yeah that's what I thought. Read the post I made on the other page, I added to it.
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
post #23 of 36
Thread Starter 
Quote:
Originally Posted by Tomiger View Post
Yeah that's what I thought. Read the post I made on the other page, I added to it.
Alright that does help a bit. So what it was doing was printing out the address it was pointing to rather than what was in that address?
post #24 of 36
Quote:
Originally Posted by Tomiger View Post
Code:
if( ! (prices[k] > 0)
   k = -1;
else
   continue
To restart the loop, you essentially need to reset the counter variable, k. If you get it to -1 when that condition is true, the loop will then increment k as it always does, and then k will be 0.
That's a bad code smell in my book. If you want an indefinite loop, use one. Ideally, if you specify bounds on iteration, they should be accurate.
TheHydra
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 @ 3.6 EVGA P55 FTW Sapphire TOXIC 2GB 6950 CORSAIR XMS3 4 x 2GB CMX4GX3M2A1600C7 
Hard DriveOSKeyboardPower
C300 64Gb + 2x F3 HD103SJ 1TB in RAID 0 Win7 x64 Razer BlackWidow Corsair 750HX 
CaseMouseMouse Pad
Lian Li A05NB with 140mm top fan. Razer DeathAdder Razer Kabuto 
  hide details  
Reply
TheHydra
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 @ 3.6 EVGA P55 FTW Sapphire TOXIC 2GB 6950 CORSAIR XMS3 4 x 2GB CMX4GX3M2A1600C7 
Hard DriveOSKeyboardPower
C300 64Gb + 2x F3 HD103SJ 1TB in RAID 0 Win7 x64 Razer BlackWidow Corsair 750HX 
CaseMouseMouse Pad
Lian Li A05NB with 140mm top fan. Razer DeathAdder Razer Kabuto 
  hide details  
Reply
post #25 of 36
Thread Starter 
Quote:
Originally Posted by blangblang View Post
That's a bad code smell in my book. If you want an indefinite loop, use one. Ideally, if you specify bounds on iteration, they should be accurate.
I don't want an indefinite loop. This is a special case. I used that =-1 trick and it works perfectly fine.
post #26 of 36
Quote:
Originally Posted by Timlander View Post
I don't want an indefinite loop. This is a special case. I used that =-1 trick and it works perfectly fine.
You've added logic that allows a loop that states bounds to become infinite. It may work for a given case, but you've added a dangerous special case.

I'm just offering caution because tricks that seem awesome in your early learning become problematic later. A little bit of thought can surely accomplish the behavior you want without a 'trick'.
TheHydra
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 @ 3.6 EVGA P55 FTW Sapphire TOXIC 2GB 6950 CORSAIR XMS3 4 x 2GB CMX4GX3M2A1600C7 
Hard DriveOSKeyboardPower
C300 64Gb + 2x F3 HD103SJ 1TB in RAID 0 Win7 x64 Razer BlackWidow Corsair 750HX 
CaseMouseMouse Pad
Lian Li A05NB with 140mm top fan. Razer DeathAdder Razer Kabuto 
  hide details  
Reply
TheHydra
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 @ 3.6 EVGA P55 FTW Sapphire TOXIC 2GB 6950 CORSAIR XMS3 4 x 2GB CMX4GX3M2A1600C7 
Hard DriveOSKeyboardPower
C300 64Gb + 2x F3 HD103SJ 1TB in RAID 0 Win7 x64 Razer BlackWidow Corsair 750HX 
CaseMouseMouse Pad
Lian Li A05NB with 140mm top fan. Razer DeathAdder Razer Kabuto 
  hide details  
Reply
post #27 of 36
Thread Starter 
Quote:
Originally Posted by blangblang View Post
You've added logic that allows a loop that states bounds to become infinite. It may work for a given case, but you've added a dangerous special case.

I'm just offering caution because tricks that seem awesome in your early learning become problematic later. A little bit of thought can surely accomplish the behavior you want without a 'trick'.
This is due in 5 minutes. So I will stick with the trick. No other choice. And I was thinking on it for about 30-45 minutes trying to figure out how to do the data validation in there and I finally posted in here cause I couldn't come up with a way. And since I don't want a user entering negative numbers for a price, making that loop infinite if they did so(ie restarting the for loop) or letting them continue if they entered a valid value was required.
post #28 of 36
Yes, the k = -1 isn't good practice, but like he said, he was short on time (and I was just leaving for class, so I just spat out the easiest/quickest solution that would probably work for his case; I didn't even look at what the loop was supposed to do)

Now that I read over what exactly the loop does, you could have done this:

I assume the loop presents an item and asks for a price. If the price isn't a positive double, you want them to re-enter it in again. Instead of making them restart the entire process (what if they entered in 4 correct prices and then messed up the last one? It'd be annoying to make the user start all over again) is to just make them re-enter in the price for that particular entry. You would have this code:

Code:
for(k = 0; k < i; k++)
{
cout << names[k] << " : ";
cin >> prices[k];
   while(prices[k] < 0)
   {
              cout << "Invalid price. Please try again\
";
 cout << names[k] << " : ";
 cin >> prices[k];
   }

}

Edited by Tomiger - 1/27/11 at 10:07pm
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
post #29 of 36
Thread Starter 
Quote:
Originally Posted by Tomiger View Post
Yes, the k = -1 isn't good practice, but like he said, he was short on time (and I was just leaving for class, so I just spat out the easiest/quickest solution that would probably work for his case; I didn't even look at what the loop was supposed to do)

Now that I read over what exactly the loop does, you could have done this:

I assume the loop presents an item and asks for a price. If the price isn't a positive double, you want them to re-enter it in again. Instead of making them restart the entire process (what if they entered in 4 correct prices and then messed up the last one? It'd be annoying to make the user start all over again) is to just make them re-enter in the price for that particular entry. You would have this code:

Code:
for(k = 0; k < i; k++)
{
   cout << names[k] << " : ";
   while( (cin >> prices[k]) < 0 )
   {
      cout << "Invalid price. Please try again\
";
   }

}
Yup I was in a big hurry lol. And guess what? Professor said we could turn it in next week now! After I slaved over that thing every day this week!!!! NOOOOOO
post #30 of 36
Hahah. Well now you have the time to make it all neat. Did you get it working with the pointers and what not? And yeah, I'd use the new code I posted up over the other one. Not only is it better programming practice, but it's much more user-friendly. (make sure you use the freshly edited code in my post; the one in your quote won't really work, as I figured out when i compiled).
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
 
Mineral Oil PC
(7 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 4790K Devil's Canyon 4.0GHz ASUS Sabertooth Z97 Mark S (White) Zotac GTX 760 Avexir Raiden 16GB DDR3 
Hard DriveCoolingCoolingCooling
OCZ Trion 240GB XSPC Raystorm Pro (white) XSPC EX 240 (white) XSPC EX 360 (white) 
CoolingCoolingCoolingCooling
Swiftech MCP655 Monsoon Dual Bay Reservoir Monsoon Modular Tube Reservoir Monsoon Stand Alone Pump Top/Cover 
CoolingPower
EK GTX 970 Water Block Corsair AX760i 
CPUMotherboardGraphicsGraphics
Intel Core i7 2600K Sandy Bridge 3.4 GHz Gigabyte GA-P67A-UD4-B3 GTX 285 GTX 285 
RAMHard DriveHard DriveCooling
Corsair Vengeance 16GB Western Digital 1TB Western Digital 250GB EK-Supremacy Clean CSQ - Nickel 
CoolingCoolingCoolingCooling
XSPC RX 360 XSPC EX 360 (cross flow) FrozenQ T-virus Reservoir EK Waterblock GTX 285 
Cooling
Swiftech MCP655 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Passing data between functions?