• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Really? Most of those I know aren't versed into JS, as far as I know. There's a huge need in anything lossely related to internet, and I know node.js can be used for many other things, but isn't there plently of fields where it doesn't matter?

Well, sure, if you go into say embedded javascript isnt necessary. And it might also be a regional thing, but if i see what my developer friends are building for their jobs its all web-based stuff, from internal company apps to websites to mobile stuff. Its javascript everywhere.
 

Caayn

Member
I'm an idiot. The first time I started working with function pointers in C++, I've been staring at this code for far too long before I realized my error...

obj->*(f); // This shows up fine in an IDE (clion) but will result in a compile error.
obj->*(f)(); // This it the correct version.
 
Guys, help me out with something that may be very dumb.

I'm starting to use ARToolKit into Unity, since I need to use it to detect a marker on a single image. That's fine and all, I created my marker, captured a pic, loaded it into the example scene and then I'm greeted with this:


p30wDhB.png

So, it's finding my marker and displaying the cube. In a black void.

I want to also display the source image instead of this black background. Is there any dumb configuration flag that I missed or to do what I want I need something a little more complex?

Thanks in advance for anyone willing to help.
 

Lois_Lane

Member
Hi, guys.

I have been working on this weird extra credit assignment my teacher assigned me for the last three weeks and have no clue what to do. He wants me to take a long string of digits, since the inputted digits are bigger than either double or int can take, and then put them in a method where they go through an alternating sum sequence. This is in Javascript by the way.

Ex. Input 11111122233334444
Alternating sum: 1-1+1-1+1-1+2-2+2-3+3-3+3-4+4-4+4= The answer.

I would use an if/else statement or a while/do while loop but everything but the forloop is forbidden. I have tried to use all the replace type methods, parseInt(), and nothing is working.

Please help
 

vypek

Member
Hi, guys.

I have been working on this weird extra credit assignment my teacher assigned me for the last three weeks and have no clue what to do. He wants me to take a long string of digits, since the inputted digits are bigger than either double or int can take, and then put them in a method where they go through an alternating sum sequence. This is in Javascript by the way.

Ex. Input 11111122233334444
Alternating sum: 1-1+1-1+1-1+2-2+2-3+3-3+3-4+4+4+4= The answer.

I would use an if/else statement or a while/do while loop but everything but the forloop is forbidden. I have tried to use all the replace type methods, parseInt(), and nothing is working.

Please help

Um, if I'm understanding this correctly...

I'd have a single var to hold the final answer. I would use a for loop that lasts the length of the input string. Then as I go through the loop, take the given index and find out if I need to add or subtract the value and perform the necessary operation on that var that holds the answer with the parseInt() of my index

Something like that?

We have a lot of smart devs here so I'm sure someone else can offer another idea
 

Lois_Lane

Member
Um, if I'm understanding this correctly...

I'd have a single var to hold the final answer. I would use a for loop that lasts the length of the input string. Then as I go through the loop, take the given index and find out if I need to add or subtract the value and perform the necessary operation on that var that holds the answer with the parseInt() of my index

Something like that?

We have a lot of smart devs here so I'm sure someone else can offer another idea

I understood you until the given index part. I have tried to do it the way you discussed it but have gone nowhere and i can't tell if my syntax is wrong or just my logic. Here's what it looks like:

public class Eleven {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
String number = key.nextLine();
String y = alternatingSum(number);
//System.out.print(y);

}

public static String alternatingSum(String number) {
for(int i=0; i<number.length(); i++){
int a = Integer.parseInt(number.charAt(i));
int b=0;
b= a+b;
int d= Integer.parseInt(number.charAt(i+1));
int f=0;
f= d+f;
}
return number; }
}
I can't see where I'm going wrong.
 

vypek

Member
I understood you until the given index part. I have tried to do it the way you discussed it but have gone nowhere and i can't tell if my syntax is wrong or just my logic. Here's what it looks like:

public class Eleven {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
String number = key.nextLine();
String y = alternatingSum(number);
//System.out.print(y);

}

public static String alternatingSum(String number) {
for(int i=0; i<number.length(); i++){
int a = Integer.parseInt(number.charAt(i));
int b=0;
b= a+b;
int d= Integer.parseInt(number.charAt(i+1));
int f=0;
f= d+f;
}
return number; }
}
I can't see where I'm going wrong.

Are you supposed to return a string or a number? At first glance, I'd change the variable y to be return type of int and have public int alternatingSum() and user Integer.parseInt(i) or myabe have the alternatingSum() be void method

EDIT: What errors are you getting by the way? I'm guessing that i+1 can cause a problem at some point for you

I was thinking something along the lines of:

int answer = 0;
for length of input
answer += parseInt(input.index)


Obviously you'll need to choose when to use += and -=

EDIT 2:

Gonna take a closer look at this when I get home tonight and try it out myself
 

msv

Member
I understood you until the given index part. I have tried to do it the way you discussed it but have gone nowhere and i can't tell if my syntax is wrong or just my logic. Here's what it looks like:

public class Eleven {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
String number = key.nextLine();
String y = alternatingSum(number);
//System.out.print(y);

}

public static String alternatingSum(String number) {
for(int i=0; i<number.length(); i++){
int a = Integer.parseInt(number.charAt(i));
int b=0;
b= a+b;
int d= Integer.parseInt(number.charAt(i+1));
int f=0;
f= d+f;
}
return number; }
}
I can't see where I'm going wrong.
You're initializing f and b in the loop and doing nothing with them. parseInt just reads the character in number at that index, and returns the integer it represents - it doesn't do anything to number itself. Yet you're returning number.

You can do it in many ways, but here's a simple one:

First, outside the loop you initialize an integer which is the result you will return. Then you initialize a boolean outside of the loop.

Then inside the loop, just do one parseInt charAt, then do an if where you check if the boolean is true > the add the integer that's parsed to the integer that you initialized outside the loop, and false > do the same but substract. Then also inside the loop, set the boolean to the negation of itself.

Then at the end, return the integer that you initialized outside the loop. That's all. If you want to return a string, do: return "" + theresultinginteger, or if you want to return an integer, change he return type of your method to int instead of string.
 

Lois_Lane

Member
Are you supposed to return a string or a number? At first glance, I'd change the variable y to be return type of int and have public int alternatingSum() and user Integer.parseInt(i) or maybe have the alternatingSum() be void method

EDIT: What errors are you getting by the way? I'm guessing that i+1 can cause a problem at some point for you

I was thinking something along the lines of:

int answer = 0;
for length of input
answer += parseInt(input.index)


Obviously you'll need to choose when to use += and -=

EDIT 2:

Gonna take a closer look at this when I get home tonight and try it out myself

Sorry I didn't clarify. I am supposed to return a string. Also I can't turn my method into any other type. He exclusively wanted it to be a String method and I can not use any if/else statements. If I could then I would have finished this thing yesterday.

My code just throws a Name exception every time I try it.

You're initializing f and b in the loop and doing nothing with them. parseInt just reads the character in number at that index, and returns the integer it represents - it doesn't do anything to number itself. Yet you're returning number.

You can do it in many ways, but here's a simple one:

First, outside the loop you initialize an integer which is the result you will return. Then you initialize a boolean outside of the loop.

Then inside the loop, just do one parseInt charAt, then do an if where you check if the boolean is true > the add the integer that's parsed to the integer that you initialized outside the loop, and false > do the same but substract. Then also inside the loop, set the boolean to the negation of itself.

Then at the end, return the integer that you initialized outside the loop. That's all. If you want to return a string, do: return "" + theresultinginteger, or if you want to return an integer, change he return type of your method to int instead of string.

I can't use if/else statements.
 

Lois_Lane

Member
You can multiply by +/-1 instead. So something like:

int answer = 0;
int factor = 1;
for integers in string
answer += factor * parseInt(input.index);
factor *= -1;

Would that be inside the forloop?

Also I can't use parseInt because my string would be too long in integer form.
 

vypek

Member
Would that be inside the forloop?

Also I can't use parseInt because my string would be too long in integer form.

The lines of what is in the for loop are the lines below the "for integers in string" in pseudo. ParseInt is what you should use and can use because you want to parse out a part of the string and turn it to a number.

For example, entering 152, in the for loop you will parse the string 1 and turn it into an int, then 5 and then 2. You want to parse out the numbers of the string and then do the mathematical operation and then turn the resulting number back into a string to return it.

The reason you are allowed to use a for loop is specifically to pick apart the string to find what numbers are inside of it so you can operate on them
 

Lois_Lane

Member
The lines of what is in the for loop are the lines below the "for integers in string" in pseudo. ParseInt is what you should use and can use because you want to parse out a part of the string and turn it to a number.

For example, entering 152, in the for loop you will parse the string 1 and turn it into an int, then 5 and then 2. You want to parse out the numbers of the string and then do the mathematical operation and then turn the resulting number back into a string to return it.

The reason you are allowed to use a for loop is specifically to pick apart the string to find what numbers are inside of it so you can operate on them

Okay I get you but can you explain .index? I can't seem to find the method anywhere in Java.
 

vypek

Member
Okay I get you but can you explain .index? I can't seem to find the method anywhere in Java.

Index is the position you are in the loop.

You can think of it as "i" in the for loop you wrote before.

for (int i = 0; i < foo.length; i++)
{
// i is the current index here
System.out.println(i);
}

Does it make more sense?

You want to pull the value at an index
 

Lois_Lane

Member
Index is the position you are in the loop.

You can think of it as "i" in the for loop you wrote before.

for (int i = 0; i < foo.length; i++)
{
// i is the current index here
System.out.println(i);
}

Does it make more sense?

You want to pull the value at an index

I'm sorry no. Here's what my code looks like now.

public class Eleven {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
String number = key.nextLine();
String y = alternatingSum(number);
System.out.print(y);


}

public static String alternatingSum(String number) {
int answer = 0;
int factor = 1;
for (int i=0; i< number.length(); i++) {
answer += factor * Integer.parseInt(number.substring(i));
factor *= -1;
}


return number; }
}

It runs fine at the start but ends up throwing an Error like this:

Exception in thread "main" java.lang.NumberFormatException: For input string: "9999999999"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
at java.lang.Integer.parseInt(Integer.java:615)
at Eleven.alternatingSum(Eleven.java:23)
at Eleven.main(Eleven.java:13)

Also can you point me to any resources I can learn more about this part of java?
 

vypek

Member
I'm sorry no. Here's what my code looks like now.



It runs fine at the start but ends up throwing an Error like this:



Also can you point me to any resources I can learn more about this part of java?

You were on a better track when you were using charAt. Passing a single argument to substring() means it will catch everything after that.

So you are grabbing too many parts of the string. So you either need to get the character at a location and have it as a string or specify the begin and end of the substring you are grabbing.
 

Lois_Lane

Member
You were on a better track when you were using charAt. Passing a single argument to substring() means it will catch everything after that.

So you are grabbing too many parts of the string. So you either need to get the character at a location and have it as a string or specify the begin and end of the substring you are grabbing.

So I have to split it up. Like something like:
number.charAt(0,i);
?
 

vypek

Member
You have completely lost me. What's the difference between the substring(i) and the charAt(0,i)? And why doesn't the program add/subtract to itself.

chatAt - returns a char at a specific part of the string
substring - returns a string of one OR MORE characters

You need to use either one of these to grab one value at a time.

chatAt(3) //returns chat at index 3
substring(2) //returns sub string from index 2 forwards
substring(2, 4) // returns sub string from index 2 to 4
 

JesseZao

Member
You have completely lost me. What's the difference between the substring(i) and the charAt(0,i)? And why doesn't the program add/subtract to itself.

Check out the documentation for those functions if you aren't using an IDE that displays it. This should be a common habit to get used to as you're learning a language/framework.
 

Lois_Lane

Member
Okay so I have finally got my code to print things out but I can't get it to pump out the right answers.

Here are my test cases:

919293949596979899= 36
9999999999=0

And my codes look like this:
public class Eleven {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
String number = key.nextLine();
String y = alternatingSum(number);
System.out.print(y);


}

public static String alternatingSum(String number) {
int answer = 0;
int factor = -1;
String z="";
for (int i = 0; i <= number.length(); i++) {
answer += factor * Integer.parseInt(number.substring(0, 2));
number = Integer.toString(answer);

System.out.print(number);
}
return number;

}
}

Thank you tempest03 and everyone else for helping me out so much I couldn't do this without you. Please help me out to the homestretch. Also I don't know where to look to get the documentation. I'm using Jetbrain's IDEA compiler thing and when I use the quick documentation rule, it doesn't do anything.
 

Caayn

Member
Thank you tempest03 and everyone else for helping me out so much I couldn't do this without you. Please help me out to the homestretch. Also I don't know where to look to get the documentation. I'm using Jetbrain's IDEA compiler thing and when I use the quick documentation rule, it doesn't do anything.
In Intellij press ctrl+q and the documentation for the current item in focus will be shown.

This is how it looks like in Intellij (Jetbrain's java IDE)
intellijjavadocc5uqk.png


As to finish the assignment, I suggest you try to use the debugger and go through the application step for step ;) It'll show you exactly what happens when and why it happens.
 
Does it make an important difference if I use a while loop there instead of an if statement? They both work the same in that situation. I just used while because the chapter is about loops.

No way to write it with two for loops though? Or would that just be inefficient?

What's wrong with the loop boundaries?

sorry i'm a noob
 

vypek

Member
Okay so I have finally got my code to print things out but I can't get it to pump out the right answers.

Here are my test cases:

919293949596979899= 36
9999999999=0

And my codes look like this:
public class Eleven {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
String number = key.nextLine();
String y = alternatingSum(number);
System.out.print(y);


}

public static String alternatingSum(String number) {
int answer = 0;
int factor = -1;
String z="";
for (int i = 0; i <= number.length(); i++) {
answer += factor * Integer.parseInt(number.substring(0, 2));
number = Integer.toString(answer);

System.out.print(number);
}
return number;

}
}

Thank you tempest03 and everyone else for helping me out so much I couldn't do this without you. Please help me out to the homestretch. Also I don't know where to look to get the documentation. I'm using Jetbrain's IDEA compiler thing and when I use the quick documentation rule, it doesn't do anything.

So if your program is running but the output is not what you are expecting then that tells you that there is a problem with your code's logic (logic error).

Take a look at the pseudo code that ricki42 showed you earlier. See if you can tell if there is a problem with your math or the numbers you are pulling during the loop. The issue must be something you are missing in the alternatingSum() function. I'm not completely sure what your professor said but it seems like you might be doing some stuff you don't need. Like, what are you using String z for? And why print out number each time during the for loop? And so you know, in the main method you could also do System.out.println(alternatingSum(...)); since you are returning a string to println.

By the way, has your class talked about debugging? Do you know how to step through the program with breakpoints?
 

Krejlooc

Banned
So I have a multiplatform game engine I've written, that I used as a base to build a level editor out of for a game to use said engine. The engine was written in C, but when I wrote the level editor, I used C++ and kind of was sloppy with dynamic allocation.

...goddamn. That lead to 11 hours of rewriting my editor to create memory pools to handle memory fragmentation. I should have just stuck with C-style memory management, lol.
 

squidyj

Member
Maybe someone can help with this.
I'm not so great at haskell.

i have an abstract syntax tree composed of 4 types (program functions, regular expressions and boolean expressions) the expressions have a lot of data constructors

I ultimately want to perform lambda lifting on the AST, the logic of which i understand.
My issue is trying to find a way to not just write the same traversal structure over and over again.

trying to do fun things leads me to rigid type errors and I don't know what else i can dp to clean up a lot of messy code
 

diaspora

Member
There's something that confuses me about the following problem:
2) Use vectors to solve the following problem. A company pays its salespeople on a commission basis.
The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. The vector should contain Employee object that contain:
2 string members for First and Last name
1 double to record Gross Sales for the current week
1 double for the weekly income initialized to 200
1 double for the bonus received based on the Gross Sales
For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $5000, or a total of $650.
Write a program (using a stl::vector) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount). Your main program should create a test case including at least 15 employee (the 15 Employee structures can be hard coded inside the main). The main program should then display the range and the number of employee within that range.
a. $200–299
b. $300–399
c. $400–499
d. $500–599
e. $600–699
f. $700–799
g. $800–899
h. $900–999
i. $1000 and over

Is this asking for objects or structures?
 

Ambitious

Member
There's this local club I visit pretty frequently. They have their event schedule on their website, but I would rather have it in my calendar instead. I would like to either a) be able to import all scheduled events manually by running a script or b) create a calendar service which I can subscribe to. Could someone please give me some hints on how to implement this? I'm gonna work with Python, I guess.

Parsing the website shouldn't be a problem. Unfortunately, the starting time for the events isn't included in the event list; it's part of the event description. So that's gonna be a bit messy, but whatever. Is the BeautifulSoup library still to be recommended?

Regarding option A: This should be pretty trivial. I just have to create an .ics file from the scraped data, which I'm sure there exist countless libraries for, and open it. Right? Might have to figure out a way to avoid duplicated items when re-running the scraper and how to deal with updated dates/times.
Obviously, I would have to manually execute the script every time they add new events to the schedule.

Regarding option B: This is of course what I would prefer, for the sake of automation and convenience. So instead of creating an .ics file, I would have to publish the scraped event data to a server. What kind of technologies do I need for this? Is it CalDAV? Any suggestions for libraries?
I have a macOS server* at home, so I could run the calendar service on there and create an automated job to scrape the website periodically.


* It's standard macOS though, not the server version.
 
I'm not sure what is going wrong here, but it seems like all my struct pointers are pointing to the same struct.

Code:
//generate structs
int numStr = 0;
int taken[10] = { 0 };
int i;
while (numStr < 7){
i = rand() % 10; //number 0-9
        if (taken[i] == 0){ 
                taken[i] = 1; 
                struct mystruct r;
                crStruct(&r, i); //initalize
                //printf("%s\n", r.name);
                used_struct[numStr] = &r; //add to used struct array
                numStr++;
        }
};

int j;

for (j=0; j < 7; j++){
printf("%s\n", used_struct[j]->name);
}

If I do: //printf("%s\n", r.name); during each struct initalization process I get different names.

Once it gets to the for loop at the end though, all the struct names are now the same. But my creation while loop should be generating a new different struct each time. So somehow the struct name is getting overwritten or my pointers are all pointing towards the same struct.

In case it helps:

Code:
void crStruct(struct myStruct *r, int n){
r->name = struct_names[n];
}


edit: Changing my used_struct array to take structs rather than pointers to structs fixed this issue.
 

Kieli

Member
Is it possible to declare a Hashtable where the value in the key-value is a primitive?

E.g. Hashtable<int, boolean> visited = new Hashtable<int, boolean>();

if (visited.get(someValue) == true) {do something};
 

Antagon

Member
Is it possible to declare a Hashtable where the value in the key-value is a primitive?

E.g. Hashtable<int, boolean> visited = new Hashtable<int, boolean>();

if (visited.get(someValue) == true) {do something};

Are you talking about Java? Then no. Generics have to be objects.

You can use the Integer and Boolean objects though. It's a bit of a weird thing in Java, but you can then use the primitives to actually fill the map.

IE: This will give compile errors on the first line, because int and boolean are primitives and not objects.

Code:
HashTable<int, boolean> visited = new HashTable<>();
visited.put(1, true);

But this will work fine:

Code:
HashTable<Integer, Boolean> visited = new HashTable<>();
visited.put(1, true);

Just make sure to do null checks if you obtain stuff from the table, since Integer and Boolean can be null.
 
Kind of want to branch out from Java. Thinking of learning some Ruby. Anyone have experience with it?

Why pick a language that is so similar to Java? Ruby is still just another OOP language. Go for something different like Clojure (which has great Java interop so your knowledge of Java is useful) or Haskell or even crazier like Prolog or APL.

If its a matter of wanting a dynamic language you are better of with Python, as its better in pretty much anything. Ruby's only strong point is Rails.
 
I'm having a hell of a hard time figuring out how to extract specific data from a file in C.

Lets say I have this text file. Or text files in this format.

Code:
Type: Mammal
Habitat 1: Forest
Habitat 2: Mountain
Diet: Carnivore

Code:
Type: Fish
Habitat 1: Ocean
Diet: Algae

I need to extract the information following ":" from each line. And print it.

So basically open each file in turn from this directory and print:

Code:
Mammal
Lives in: Forest, Mountain.
Carnivore

Code:
Fish
Lives in: Ocean
Algae

Is there even an easy way to do this in C?
 

Allonym

There should be more tampons in gaming
Hey everyone, I'm new to programming and taking an introductory computer science course at my University. I was hoping someone could chime in and potentially help me out with some problems I'm having with an assignment;

Sevens are considered lucky numbers. Your task is to count the number of sevens that appear within a range of numbers. Your solution should make use of looping constructs.

Ask the user for the following information, in this order:

The lower end of the range
The upper end of the range

Determine the number of sevens that appear in the sequence from lower end to upper end (inclusive).
Hint: Some numbers have more than 1 seven, and not every 7 appears in the ones place.
Hint2: Nested loops are helpful

Exit on error.
--------------------------------------------------------------------------------------------------------------------------------
The example goes on to list an upper and lower range, the lower range being 100 and the upper range being 150 and the result was 5. This is where I'm somewhat lost or having difficulty wrapping my head around because I'm fairly certain there are 7 "5's" between 100 and 150, ie 107, 114, 121, 128, 135, 142, 149. It's just incredibly confusing. Maybe I'm thinking about this all wrong.

Any help would be appreciated.

EDIT; Damn I just realized they counted the literal "7's" that appeared and not intervals of 7. Still I'm having difficulty understanding how I'd use a nested loop to create the program that counts the number of 7's in a range I'd make
 

Pokemaniac

Member
I'm having a hell of a hard time figuring out how to extract specific data from a file in C.

Lets say I have this text file. Or text files in this format.

Code:
Type: Mammal
Habitat 1: Forest
Habitat 2: Mountain
Diet: Carnivore

Code:
Type: Fish
Habitat 1: Ocean
Diet: Algae

I need to extract the information following ":" from each line. And print it.

So basically open each file in turn from this directory and print:

Code:
Mammal
Lives in: Forest, Mountain.
Carnivore

Code:
Fish
Lives in: Ocean
Algae

Is there even an easy way to do this in C?

strtok() is often the easiest way to break up a string in C. Though, be careful to read the documentation before using it. The function has some subtleties you need to be aware of.

As for getting the data from the file itself. I'd be inclined to use getline(), but I'm pretty sure that's POSIX only, so that's not going to work if you're targeting Windows.
 
Ok I've ALMOST solved it... almost.

I'm having an issue saving my strings from the token.

But basically if I print my tokens as they are generated I get

"mammal"
"forest"
"mountain"
etc...

but when I try to save them to an array of strings, and then iterate over that array, ALL the strings in the array end up as the last token read from the file.

so basically
"mammal' gets read, and I try to add it to my array of strings.
at the end of the file "carnivore" gets read.
Then my array somehow ends up like: ["carnivore", "carnivore", "carnivore"]
 

vypek

Member
Hey everyone, I'm new to programming and taking an introductory computer science course at my University. I was hoping someone could chime in and potentially help me out with some problems I'm having with an assignment;

Sevens are considered lucky numbers. Your task is to count the number of sevens that appear within a range of numbers. Your solution should make use of looping constructs.

Ask the user for the following information, in this order:

The lower end of the range
The upper end of the range

Determine the number of sevens that appear in the sequence from lower end to upper end (inclusive).
Hint: Some numbers have more than 1 seven, and not every 7 appears in the ones place.
Hint2: Nested loops are helpful

Exit on error.
--------------------------------------------------------------------------------------------------------------------------------
The example goes on to list an upper and lower range, the lower range being 100 and the upper range being 150 and the result was 5. This is where I'm somewhat lost or having difficulty wrapping my head around because I'm fairly certain there are 7 "5's" between 100 and 150, ie 107, 114, 121, 128, 135, 142, 149. It's just incredibly confusing. Maybe I'm thinking about this all wrong.

Any help would be appreciated.

EDIT; Damn I just realized they counted the literal "7's" that appeared and not intervals of 7. Still I'm having difficulty understanding how I'd use a nested loop to create the program that counts the number of 7's in a range I'd make

The outer loop iterates over the range like 100 to 150. The nested loop is to go over the length of a specific number. For instance you are on index 107, the nested loop goes over the ones place, then the tens and then hundreds place. Once that is finished the outer loop iterates to 108. The inner loop now checks the same places and reads an 8, 0 and 1.

The nested loop is just to scan for the 7's. The outer loop is to move to the next item in the range.

Hope that helps.
 

Pokemaniac

Member
Ok I've ALMOST solved it... almost.

I'm having an issue saving my strings from the token.

But basically if I print my tokens as they are generated I get

"mammal"
"forest"
"mountain"
etc...

but when I try to save them to an array of strings, and then iterate over that array, ALL the strings in the array end up as the last token read from the file.

so basically
"mammal' gets read, and I try to add it to my array of strings.
at the end of the file "carnivore" gets read.
Then my array somehow ends up like: ["carnivore", "carnivore", "carnivore"]

Sounds kind of like you might be accidentally saving a pointer to a pointer instead of just the pointer itself.
 

Allonym

There should be more tampons in gaming
The outer loop iterates over the range like 100 to 150. The nested loop is to go over the length of a specific number. For instance you are on index 107, the nested loop goes over the ones place, then the tens and then hundreds place. Once that is finished the outer loop iterates to 108. The inner loop now checks the same places and reads an 8, 0 and 1.

The nested loop is just to scan for the 7's. The outer loop is to move to the next item in the range.

Hope that helps.

Have I ever told you, you're my hero? You're everything, everything I wish I could be...but in all seriousness thank you. I really appreciate it. Why can't the Professors and TA's say this shit simply?
 
Sounds kind of like you might be accidentally saving a pointer to a pointer instead of just the pointer itself.

Ok now I have all of it working. My array is set up properly.

Now I'm having trouble with printf of all fuckin things.

I need my shit to print like this:

Code:
Mammal
Lives in: Forest, Mountain.

but it is printing like this:
Code:
Mammal

Lives in: Forest
, Mountain.

Literally no clue why...

Code:
int i;
printf("%s\n", array[0]);
printf("Lives in: ");
for (b=1; b < a; b++)
{
printf("%s, ", filearray[b]);
}

ignore that the stuff in the for loop is technically wrong for printing the last item with a period instead of a comma. The interesting bits here are: an extra newline appearing. the first "," not printing after the %s, but instead on a newline after the s...

midpost edit: Oh fuck.................... My strings must be copying to the array with a newline already in them. How do I fix that?
 

Pokemaniac

Member
Ok now I have all of it working. My array is set up properly.

Now I'm having trouble with printf of all fuckin things.

I need my shit to print like this:

Code:
Mammal
Lives in: Forest, Mountain.

but it is printing like this:
Code:
Mammal

Lives in: Forest
, Mountain.

Literally no clue why...

Code:
int i;
printf("%s\n", array[0]);
printf("Lives in: ");
for (b=1; b < a; b++)
{
printf("%s, ", filearray[b]);
}

ignore that the stuff in the for loop is technically wrong for printing the last item with a period instead of a comma. The interesting bits here are: an extra newline appearing. the first "," not printing after the %s, but instead on a newline after the s...

midpost edit: Oh fuck.................... My strings must be copying to the array with a newline already in them. How do I fix that?

All you need to do is just find the new line ('\n' on Linux/Unix or '\r' followed by '\n' on windows) and replace it with something else. Since the character(s) you want to replace will be at the end of the string, it is safe to replace it/them with null bytes, since (if I understand what you're doing correctly) there is nothing in the string after them that you have to worry about.
 
Top Bottom