Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 329 330 [331] 332 333 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 908238 times)

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4950 on: September 19, 2013, 11:18:24 am »

Huh, I'm pretty sure I actually used something from that header. I guess it's gone now.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4951 on: September 19, 2013, 01:32:17 pm »

Is there a way for me to write a loop that prints all of the values from the getHoldings function?
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4952 on: September 19, 2013, 01:37:08 pm »

Is there a way for me to write a loop that prints all of the values from the getHoldings function?

Code: [Select]
for (auto holding: data) {
cout << holding.first << " " << holding.second << endl;
}
Should work.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4953 on: September 19, 2013, 01:52:23 pm »

Is there a way for me to write a loop that prints all of the values from the getHoldings function?

Code: [Select]
for (auto holding: data) {
cout << holding.first << " " << holding.second << endl;
}
Should work.

That prints out all the holdings, but it doesn't look very good. To make it look good, you could make a static map<string, string> langMap of translations, mapping "sRawMithril" to "Raw Mithil" and similar, then use langMap[holding.first] instead of holding.first. If you read that translation from an external text file, you could even have multiple language files in different languages to choose from.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4954 on: September 19, 2013, 03:09:18 pm »

Code: [Select]
        int getHoldings(string strEndRead)
        {
ifstream fin("EmpireHoldings.dat");
                istringstream is();
                while (true) {
                        string key;
                        int value;
                        is.str(fin.getline());
                        is >> key;
                        if (key == strEndRead) break;
                        is >> value;
                        data[key] = value;
                }
        }

My compiler tells me "left of '.str' must have class/struct/union." I did #include <sstream> in case that might have been the cause.

E: I think it's because the declaration of is has parentheses. I got a different error when I removed them though.

"'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 0 arguments"
« Last Edit: September 19, 2013, 03:15:50 pm by MrWillsauce »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4955 on: September 19, 2013, 04:10:01 pm »

Okay, so getline works differently than I thought:
Code: [Select]
        int getHoldings(string strEndRead)
        {
ifstream fin("EmpireHoldings.dat");
                istringstream is;
                while (true) {
                        string key;
                        int value;
                        string line;
                        getline(fin, line);
                        is.str(line);
                        is >> key;
                        if (key == strEndRead) break;
                        is >> value;
                        data[key] = value;
                }
        }
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4956 on: September 19, 2013, 04:53:53 pm »

Are you sure 'is' should be used to assign a value to 'value'? I don't think we can give value a string.
Logged

Lightningfalcon

  • Bay Watcher
  • Target locked. Firing main cannon.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4957 on: September 19, 2013, 06:16:01 pm »

I finally finished code for a problem I was given in my AP CompSci class just now.  The purpose is to "Write a function that takes and integer argument.  Return true if the number is divisible by all of its digits (if a digit is 0 return false) else retuen false" Can you guys tell me how badly I messed this up-
Spoiler (click to show/hide)
Also are there are any ways to improve the efficiency of this?  And are there any ways to input a number to break this?
Logged
Interdum feror cupidine partium magnarum circo vincendarum
W-we just... wanted our...
Actually most of the people here explicitly wanted chaos and tragedy. So. Uh.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4958 on: September 19, 2013, 06:21:38 pm »

Are you sure 'is' should be used to assign a value to 'value'? I don't think we can give value a string.
Since 'value' is of type int, the >> operator extracts an integer definition from the stringstream and stores that in 'value'.

Edit for Lightningfalcon:
Code: [Select]
public static boolean divstuff (int basenum) {
int num = basenum;
while (num > 0) {
int digit = num % 10;
if (digit == 0 || basenum % digit != 0) return false;
num /= 10;
}
return true;
}
« Last Edit: September 19, 2013, 06:27:45 pm by MagmaMcFry »
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4959 on: September 19, 2013, 06:26:59 pm »

Well when I try to compile the program, I get the error "binary '[' : no global operator found which takes type 'std::string' (or there is no acceptable conversion)," referring to data[key] = value;
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4960 on: September 19, 2013, 06:29:37 pm »

I guess that means that data has no operator[](string). Weird. Can you post your entire code, just to be sure?
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4961 on: September 19, 2013, 06:30:32 pm »

Code: [Select]
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
using namespace std;



struct Structures
{
int farms;
int subterraneanFarms;
int mines;
int forges;
int naturePreserves;
int stables;
int unicornPens;
int fisheries;
int fortresses;
int towns;
int graveyards;
int towers;
};
struct Stocks
{
int food;
int wood;
int horses;
int coal;
int sulfur;
int ironOre;
int ironBars;
int ironWeapons;
int ironArmor;
int steelBars;
int steelWeapons;
int SteelArmor;
int rawMithril;
int mithrilBars;
int mithrilWeapons;
int mithrilArmor;
int unicorns;
int humanPopulation;
int elfPopulation;
int dwarfPopulation;
int koboldPopulation;
int hobbitPopulation;
};
struct Technologies
{
bool mining;
bool mithrilMining;
bool blastingCharges;
bool fieldAgriculture;
bool subterraneanFarming;
bool horseDomestication;
bool unicornDomestication;
bool preservation;
bool fishDomestication;
bool woodcutting;
bool smelting;
bool steelsmelting;
bool armoring;
bool weaponSmithing;
bool mithrilSmelting;
bool mithrilSmithing;
bool charcoalBurning;
bool theft;
bool masterTheft;
bool stealthManeuvering;
bool fortress;
bool wallConstruction;
bool sailing;
int researchPoints;
};
struct Population
{
int population;
};
class Holdings
{
public:
map <string, int> data();

//takes data from EmpireHoldings.dat and assigns it to variables
int getHoldings(string strEndRead)
{
ifstream fin("EmpireHoldings.dat");
                istringstream is;
                while (true) {
                        string key;
                        int value;
                        string line;
                        getline(fin, line);
                        is.str(line);
                        is >> key;
                        if (key == strEndRead) break;
                        is >> value;
                        data[key] = value;
                }
}

//changes variables based on HFC Input.dat
int getInputs()
{
return 0;
}
//prints up-to-date data to EmpireHoldings.dat
int printHoldings()
{
return 0;
}

};
int main()
{
Holdings HumanHoldings;
HumanHoldings.getHoldings("endHuman");
return 0;
}
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4962 on: September 19, 2013, 06:36:45 pm »

Oh, whoops.
Code: (Quick fix) [Select]
map <string, int> data;
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4963 on: September 19, 2013, 06:40:42 pm »

That's what I get for just copying things >.< thanks.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4964 on: September 19, 2013, 06:53:42 pm »

How would I return all of the values that I fed to the map at the end of the function? Just 'return data'?
Logged
Pages: 1 ... 329 330 [331] 332 333 ... 796