Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
FamDav
Mar 29, 2008

Zopotantor posted:

Actually :eng101: void is the type that is called 'unit' in functional languages. Void functions return to their caller, but the 'value' they return is not used. Bottom is the conceptual return type of functions that don't return, not even a useless value.

C++ doesn't allow the unit value to be passed around, so you can't do this:
code:
void fart(void);
void butt(void);
void dick(void)
{
  return fart(butt()); // nope
}
The value of a throw expression is treated as void syntactically (I think; at least the description of the conditional expression treats it that way). This is about as theoretically consistent as the rest of C++, so nobody really cares (except for pedants like myself :eng99:).

void isn't unit, but its also not really bottom either. I can't put something of type void in a struct (whereas i could put something of type struct Unit {}; ) so nothing inhabits its type unless you kind of pretend it does. but I also can write a series of functions that that all return void and it type checks and executes.

thats probably because c++ is c++ and the semicolon isnt actually a monad and thats why you have a special rule in there to make it work.

Adbot
ADBOT LOVES YOU

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

I'm trying to write a simple program that converts an entered number from 1 to 100 to roman numerals. We can't use recursion or arrays yet. The code compiles but It only prints "C" (100), regardless of the value entered for n. Why would this happen?

C code:
#include <stdio.h>

int main ()
{
    int n;


    printf("Please enter a number that does not exceed 100.\n");
    scanf("%d", &n);

while (n !=0){

            if(n=100){
                printf("C");
                n = n - 100;
                return n;
                }
                else if (n>90,n<100){
                    printf("XC");
                    n = n-90;
                    return n;
                    }
                else if (n>80,n<90){
                    printf("LXXX");
                    n = n-80;
                    return n;
                    }
                else if (n>70,n<80){
                    printf("LXX");
                    n = n-70;
                    return n;
        }
        else if (n>60,n<70){
            printf("LX");
            n = n-60;
            return n;
        }
        else if (n>50,n<60){
            printf("L");
            n = n-50;
            return n;
        }
        else if (n>40,n<50){
            printf("XL");
            n = n-40;
            return n;
        }
        else if (n>30,n<40){
            printf("XXX");
            n = n-30;
            return n;
        }
        else if (n>20,n<30){
            printf("XX");
            n = n-20;
            return n;
        }
        else if (n>10,n<20){
            printf("X");
            n=n-10;
            return n;
        }
        else if (n=9){
            printf("IX");
            n=n-9;
            return n;
        }
        else if (n=8){
            printf("VIII");
            n=n-8;
            return n;
        }
        else if (n=7){
            printf("VII");
            n=n-7;
            return n;
        }
        else if (n=6){
            printf("VI");
            n=n-6;
            return n;
        }
        else if (n=5){
            printf("V");
            n=n-5;
            return n;
        }
        else if (n=4){
            printf("IV");
            n=n-4;
            return n;
        }
        else if (n=3){
            printf("III");
            n=n-3;
            return n;
        }
        else if (n=2){
            printf("II");
            n=n-2;
            return n;
        }
        else if (n=1){
            printf("I");
            n=n-1;
            return n;
        }
    }
}

Modulo16 fucked around with this message at 19:37 on Jun 8, 2015

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Frank Viola posted:

I'm trying to write a simple program that converts an entered number from 1 to 100 to roman numerals. We can't use recursion or arrays yet. The code compiles but It only prints "C" (100), regardless of the value entered for n. Why would this happen?

The comma operator does not do what you think it does.
You need to use && instead.

Oh, and your use of "return" also looks suspicious. You may want to replace it with "continue".

Zopotantor fucked around with this message at 19:46 on Jun 8, 2015

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You also need to learn the difference between assignment (=) and equality (==).

You also probably do not want to return n from main.

Edward_Tohr
Aug 11, 2012

In lieu of meaningful text, I'm just going to mention I've been exploding all day and now it hurts to breathe, so I'm sure you all understand.
Also, look carefully at your if statements.

if(n=100) sets n to 100. You'd want two equals signs there to test for equivalence.

e;f,b

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

Zopotantor posted:

The comma operator does not do what you think it does.
You need to use && instead.

Oh, and your use of "return" also looks suspicious. You may want to replace it with "continue".

rjmccall posted:

You also need to learn the difference between assignment (=) and equality (==).

You also probably do not want to return n from main.

I have applied both changes, but the values don't correspond to the input.

i.e. input of 32 yields XXX not XXXII

JawKnee
Mar 24, 2007





You'll take the ride to leave this town along that yellow line

Frank Viola posted:

I have applied both changes, but the values don't correspond to the input.

i.e. input of 32 yields XXX not XXXII

Just to be clear here, there were three changes suggested there, did you do all three?

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

Yes. I now understand the difference between assignment and equality, why the comma doesn't do what I thought it did and why it was necessary to do &&, and that returning n is not recommended.

Continue only caused an infinite loop.

Modulo16 fucked around with this message at 20:27 on Jun 8, 2015

JawKnee
Mar 24, 2007





You'll take the ride to leave this town along that yellow line
Post the changes you made, if you change all returns to continue, and change the assignment operators in the if-statement-condition to the equality operator, as well as changing the commas to && (again in the if-condition) then it will work fine, the only time you would be stuck in the loop is if n is being set to something higher than 0 and not decremented to 0 in a reachable statement (I'm guessing you either missed an assignment operator or changed the assignment operator inside the if-block to an equality operator accidentally).

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

JawKnee posted:

Post the changes you made, if you change all returns to continue, and change the assignment operators in the if-statement-condition to the equality operator, as well as changing the commas to && (again in the if-condition) then it will work fine, the only time you would be stuck in the loop is if n is being set to something higher than 0 and not decremented to 0 in a reachable statement (I'm guessing you either missed an assignment operator or changed the assignment operator inside the if-block to an equality operator accidentally).

Check what happens if n is, e.g., 30.

JawKnee
Mar 24, 2007





You'll take the ride to leave this town along that yellow line

Zopotantor posted:

Check what happens if n is, e.g., 30.

ahh I didn't read close enough there.

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

JawKnee posted:

ahh I didn't read close enough there.



code:
#include <stdio.h>//INCLUDES I/O LIBRARY

int main ()//MAIN CLASS
{
    int n;//INTEGER VALUE N


    printf("Please enter a number that does not exceed 100.\n");//PROMPT FOR INPUT
    scanf("%d", &n);//RECIEVE INPUT

while (n != 0&&n>=10){//CONDITION OF WHILE LOOP

            if(n==100){ //CONVERSION FOR 100
                printf("C");
                (n == n - 100);//SUBRACT FROM TOTAL

                }
                else if (n>=90&&n<=100){//90-100
                    printf("XC");
                    (n == n-90);//subtract from n

                }
                else if (n>=80&&n<=90){
                    printf("LXXX");
                    (n == n-80);

                    }
                else if (n>=70&&n<=80){
                    printf("LXX");
                    (n == n-70);

                    }
                else if (n>=60&&n<=70){
                    printf("LX");
                    (n == n-60);

                    }
                else if (n>=50&&n<=60){
                    printf("L");
                    (n == n-50);

                }
                else if (n>=40&&n<=50){
                    printf("XL");
                    (n == n-40);

                }
                else if (n>=30&&n<=40){
                    printf("XXX");
                    (n == n-30);

                }
                else if (n>=20&&n<=30){
                    printf("XX");
                    (n == n-20);

                }
                else if (n>=10&&n<=20){
                    printf("X");
                    (n==n-10);

                }

                else if (n==9){
                    printf("IX");
                    (n==n-9);

                }
                else if (n==8){
                    printf("VIII");
                    (n==n-8);

                }
                else if (n==7){
                    printf("VII");
                    (n==n-7);

                }
                else if (n==6){
                    printf("VI");
                    (n==n-6);

                }
                else if (n==5){
                    printf("V");
                    (n==n-5);

                }
                else if (n==4){
                    printf("IV");
                    (n==n-4);

                }
                else if (n==3){
                    printf("III");
                    (n==n-3);

                }
                else if (n==2){
                    printf("II");
                    (n==n-2);

                }
                else if (n==1){
                    printf("I");
                    (n==n-1);

                }
        return 0;//EXIT SUCCESS
            }

}

JawKnee posted:

Post the changes you made, if you change all returns to continue, and change the assignment operators in the if-statement-condition to the equality operator, as well as changing the commas to && (again in the if-condition) then it will work fine, the only time you would be stuck in the loop is if n is being set to something higher than 0 and not decremented to 0 in a reachable statement (I'm guessing you either missed an assignment operator or changed the assignment operator inside the if-block to an equality operator accidentally).

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
== is not assignment.

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

Dessert Rose posted:

== is not assignment.

the if statements are to test for equality. if the integer satisfies the condition it prints, otherwise it doesn't why would I need to assign any number value when I am trying to get it to satisfy the conditions of 1 if or else if statement? I need it to satisfy on statement, then move down to the ranges of 1-9 if they are present in the number.

Evil_Greven
Feb 20, 2007

Whadda I got to,
whadda I got to do
to wake ya up?

To shake ya up,
to break the structure up!?
== is for testing equality, but assigns no value.

You appear to be attempting to use it to assign a value in the code.

Because of how C/C++ works, this means a statement like (and I quote):
code:
(n == n - 100);//SUBRACT FROM TOTAL
...will do nothing in assignment. n will be whatever it was before and after this statement.

Further, your return statement for your main() function is within your while loop, and will end after the first check through the if/else chain

Evil_Greven fucked around with this message at 03:36 on Jun 9, 2015

JawKnee
Mar 24, 2007





You'll take the ride to leave this town along that yellow line
You need to alter the number that you are checking at each iteration of the while loop if you ever want it to terminate (that number being n)

so if you want to check for an equality in your if-condition (if A is equal to B): if( A == B), however if you want to set a variable to be equal to some value (set n equal to n minus A): n = n - A

There are two other issues here, take a look at the condition in your while loop:

code:
while (n != 0&&n>=10)
the && operator means that BOTH conditions have to resolve as true if you want the loop to continue, so that if EITHER condition (or both) resolve as false, the loop will terminate. In this case, the loop will terminate if n is NOT equal to 0, or if n is less than 10.

finally, you have:

code:
return 0;
at the end of your while loop. A return <something>; call will immediately end the function that contains it, returning the value of <something> to the function that called the terminated function. So (without the continue calls) your function will terminate on its first pass, and since you used else-if statements rather than just a bunch of if statements, only one of them at most will be entered.

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

Evil_Greven posted:

== is for testing equality, but assigns no value.

You appear to be attempting to use it to assign a value in the code.

Because of how C/C++ works, this means a statement like (and I quote):
code:
(n == n - 100);//SUBRACT FROM TOTAL
...will do nothing in assignment. n will be whatever it was before and after this statement.

Further, your return statement for your main() function is within your while loop, and will end after the first check through the if/else chain

would n-=100 be a better choice? And how do I get the while loop to execute again?

C code:
#include <stdio.h>//INCLUDES I/O LIBRARY

int main ()//MAIN CLASS
{
    int n;//INTEGER VALUE N


    printf("Please enter a number that does not exceed 100.\n");//PROMPT FOR INPUT
    scanf("%d", &n);//RECIEVE INPUT

do {//do loop

            if(n==100){ //CONVERSION FOR 100
                n = n-100;
                printf("C");


                }
                else if (n>=90&&n<=100){//90-100
                    n = n-90;//subtract from n
                    printf("XC");

                }
                else if (n>=80&&n<=90){
                    n = n-80;
                    printf("LXXX");


                    }
                else if (n>=70&&n<=80){
                    n = n-70;
                    printf("LXX");


                    }
                else if (n>=60&&n<=70){
                    (n = n-60);
                    printf("LX");


                    }
                else if (n>=50&&n<=60){
                    n = n-50;
                    printf("L");


                }
                else if (n>=40&&n<=50){
                    n = n-40;
                    printf("XL");

                }
                else if (n>=30&&n<=40){
                    (n = n-30);
                    printf("XXX");


                }
                else if (n>=20&&n<=30){
                    n = n-20;
                    printf("XX");


                }
                else if (n>=10&&n<=20){
                    n = n-10;
                    printf("X");
                    }


            else if (n==9){
                    printf("IX");
                    (n = n-9);

                }
                else if (n==8){
                    printf("VIII");
                    (n = n-8);

                }
                else if (n==7){
                    printf("VII");
                    (n = n-7);

                }
                else if (n==6){
                    printf("VI");
                    (n = n-6);

                }
                else if (n==5){
                    printf("V");
                    (n = n-5);

                }
                else if (n==4){
                    printf("IV");
                    (n= n-4);

                }
                else if (n==3){
                    printf("III");
                    (n= n-3);

                }
                else if (n==2){
                    printf("II");
                    (n = n-2);

                }
                else if (n==1){
                    printf("I");
                    (n=n-1);

                }
        return 0;//EXIT SUCCESS
            }
while (n != 0&&n<=100);//conditional while loop
}

Modulo16 fucked around with this message at 03:42 on Jun 9, 2015

JawKnee
Mar 24, 2007





You'll take the ride to leave this town along that yellow line
while loops will continue to execute for as long as their condition resolves to true, provided it isn't explicitly broken by a break; or return <something>; call.

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

JawKnee posted:

while loops will continue to execute for as long as their condition resolves to true, provided it isn't explicitly broken by a break; or return <something>; call.

I removed the return and it solved the problem. Thanks dudes, I actually learned more from this than from the TA that teaches the class!

astr0man
Feb 21, 2007

hollyeo deuroga

Frank Viola posted:

would n-=100 be a better choice?

That would work, so would
code:
n = n - 100;

Doc Block
Apr 15, 2003
Fun Shoe
You also have the problem of doing
C++ code:

if(n==100)
...
else if(n>=90&&n<=100)

when it should be
C++ code:

if(n==100)
...
else if(n>=90&&n<100)

or even just
C++ code:

if(n==100)
...
else if(n>=90)

since n is guaranteed to be less than 100 if control reaches the else if statement.

Marta Velasquez
Mar 9, 2013

Good thing I was feeling suicidal this morning...
Fallen Rib

Doc Block posted:

C++ code:
if(n==100)
...
else if(n>=90)

If you do this, you don't even need the while loop. Just get rid of all the else keywords.

Slash
Apr 7, 2011

I'm surprised no one has attempted to do this as a one-liner yet.

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Slash posted:

I'm surprised no one has attempted to do this as a one-liner yet.

Knuth needed 20 lines for it in TeX, so I'm not going to try.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Zopotantor posted:

Knuth needed 20 lines for it in TeX, so I'm not going to try.

That's no justification.

sarehu
Apr 20, 2007

(call/cc call/cc)
code:
void roman(int x) {
   char *p="IVIIIXLXXXCDCCCMMM";
   int a[] = {0,1,2,3,2,1,2,3,4,2}, b[] = {2,2,2,2,0,1,1,1,1,4};
   printf("%.*s%.*s%.*s%.*s\n", a[x/1000%4], p+15+b[x/1000%4], a[x/100%10],
          p+10+b[x/100%10], a[x/10%10], p+5+b[x/10%10], a[x%10], p+b[x%10]);
}

sarehu fucked around with this message at 20:24 on Jun 9, 2015

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Subjunctive posted:

That's no justification.

Justification takes even more code, man.

sarehu
Apr 20, 2007

(call/cc call/cc)
code:
void roman(int x) {
  char*a="@ABCBABCDB",*b="BBBB@AAAAD";printf("%.*s%.*s%.*s%.*s\n",7&a
  [x/1000%4],"MMM"+b[x/1000%4]%8,7&a[x/100%10],"CDCCCM"+b[x/100%10]%8
  ,7&a[x/10%10],"XLXXXC"+b[x/10%10]%8,7&a[x%10],"IVIIIX"+b[x%10]%8);
}
Edit: last version:
code:
void roman(int x) {
  char*a="#$%&%$%&'%%%%#$$$$'IVIIIXLXXXCDCCCMMMM";for(int i=10000,c=1;
  i/=10;printf("%.*s",a[x/i%10]%5,a+a[9+x/i%10]-c),c+=5);
}

sarehu fucked around with this message at 21:55 on Jun 9, 2015

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

An interesting report on the status of C++17: https://botondballo.wordpress.com/2015/06/05/trip-report-c-standards-meeting-in-lenexa-may-2015/

VikingofRock
Aug 24, 2008





This was really interesting. I'm sad that modules probably aren't going to make it by C++17, but it looks like a lot of the stuff that will make it will be really good. Thanks!

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

I'm so glad I'm not the poor human who has to drive the filesystem spec.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
I'm mostly glad that they're really aiming for shipping something in 2017 rather than delaying it for big items.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Fixed-date release cycles are the truth and the light.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Subjunctive posted:

I'm so glad I'm not the poor human who has to drive the filesystem spec.
I dislike boost.filesystem (it's one of the most awkward filesystem libraries I've used other than the thin wrappers around POSIX stuff), but I was still happy to see it initially accepted mostly unchanged because if people manage to force it to support every sort of filesystem in existence it will become an unholy abomination.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Yeah, I think that it makes sense that they chose mediocre over Byzantine, it's just gotta be a bad job to navigate that process.

sarehu
Apr 20, 2007

(call/cc call/cc)

So, does "voted out for publication" mean it's in?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Yeah, means they voted to publish it as a TS, I believe.

Eeyo
Aug 29, 2004

Ok I have another (dumb) LLDB question:

Why does stepping into a method seem to land me in a random spot inside the method/source file? I've got a class with several .cpp files each with a method. When I step into one, LLDB puts me in line 81 of the file even though it should start at line ~26 or so. All of the variables I have in the class are wrong when I print them, too. Then when I step again it puts me where it should have started and then all the variables make sense.

Do I just not understand how LLDB handles stepping? I figured it should just put me right inside the method when I step into it.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Eeyo posted:

Ok I have another (dumb) LLDB question:

Why does stepping into a method seem to land me in a random spot inside the method/source file? I've got a class with several .cpp files each with a method. When I step into one, LLDB puts me in line 81 of the file even though it should start at line ~26 or so. All of the variables I have in the class are wrong when I print them, too. Then when I step again it puts me where it should have started and then all the variables make sense.

Do I just not understand how LLDB handles stepping? I figured it should just put me right inside the method when I step into it.

It might just be a bug, or there might be some implicit behavior at the start of the function that you're stepping through.

Adbot
ADBOT LOVES YOU

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
I don't know how lldb handles C++, but Visual Studio's debugger has a "step" on the opening curly brace of a class method before the this pointer gets initialized, at which point your ivars are going to be corrupt because you're accessing them through an uninitialized pointer to who knows where.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply