Jump to content


Photo

Wtf?!

c++ programing

  • Please log in to reply
33 replies to this topic

#1 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 07:04 PM

Where in the bluest blue hell is the problem?! @#$%^!

 

 

#include <stdio.h>
#include <stdlib.h>
 
main()
{
      char c;
      int result,a,b;
   
      printf("Give one of these: + - /.n");
      scanf("%c",&c);
      printf("Give an integer.n");
      scanf("%d",&a);
      printf("Give another integer.n");
      scanf("%d",&b );
   
      while ((a!=-1)&&(b!=-1))
      {
            if (c=='+')
               result=a+b;
            else if(c=='-')
                 result=a-b;
            else
                result=a/b;
         
          printf("Give one of these: + - /.n");
            scanf("%c",&c);
            printf("Give an integer.n");
            scanf("%d",&a);
            printf("Give another integer.n");
            scanf("%d",&b );
         
            printf("%d %c %d = %d",a,c,b,result);
      }
}
 
 
line: 34, message: syntax error before '}' token 

...you can achieve immortality, simply by doing one great thing...


#2 P_W_S Marcello

P_W_S Marcello

    Setti Housetroll

  • Members
  • 662 posts
314
  • LocationLol i dunno, never left my room

Posted 26 March 2013 - 07:35 PM

  while ((a!=-1)&&(b!=-1))
      {
I think correctly it would be this: (but im not sure)
 
  while ((a!=-1)&&(b!=-1)) {


#3 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 07:37 PM

It's the same. :(


...you can achieve immortality, simply by doing one great thing...


#4 Ellanios

Ellanios

    Hole Stretcher

  • Moderators - CS admin
  • 640 posts
540
  • LocationPantheon of the Hole Stretchers

Posted 26 March 2013 - 07:39 PM

#include <stdio.h>
#include <stdlib.h>
 
main()
{
      char c;
      int result,a,b;
   
      printf("Give one of these: + - /.n");
      scanf("%c",&c);
      printf("Give an integer.n");
      scanf("%d",&a);
      printf("Give another integer.n");
      scanf("%d",&b );
   
      while ((a!=-1)&&(b!=-1))
      {
            if (c=='+')
               result=a+b;
            else if(c=='-')
                 result=a-b;
            else
                result=a/b;
         
          printf("Give one of these: + - /.n");
            scanf("%c",&c);
            printf("Give an integer.n");
            scanf("%d",&a);
            printf("Give another integer.n");
            scanf("%d",&b );
         
            printf("%d %c %d = %d",a,c,b,result);
      }
return 0; <---- add this
}
 


#5 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 07:59 PM

Ok it seems that it's working now. I had added two more characters  in the while command by mistake. Thanks for the help anyway Ellanios.

 

But! Now i have another problem. The first turn works great. During the second turn, it excecutes the "printf("Give one of these: + - /.n");" and the "scanf("%c",&c);" commands, but it doesn't wait for the user to input the character! Then it excecutes the "printf("Give an integer.n");" and the "scanf("%d",&a);" command and waits for the user to input an integer.

 

In a few words, it bypasses the "scanf("%c",&c);" command.  :confused: 


...you can achieve immortality, simply by doing one great thing...


#6 Ellanios

Ellanios

    Hole Stretcher

  • Moderators - CS admin
  • 640 posts
540
  • LocationPantheon of the Hole Stretchers

Posted 26 March 2013 - 08:22 PM

Well, my C programming skills are very rusty but, I think It probably has to do with the while statement.

The variable check shouldn't be in the while statement.

 

Try setting While(1) so to keep the loop forever.

 

Do you know how to use switch() case statement? I think is more neat way to write this program.



#7 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 08:26 PM

Is there any possibility the variable "c" keeps its value since the first turn? So to bypass the input during the second turn?


...you can achieve immortality, simply by doing one great thing...


#8 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 08:30 PM

switch() case statement?

Do you mean the "case" command which has similar function to the "if" command?


...you can achieve immortality, simply by doing one great thing...


#9 Ellanios

Ellanios

    Hole Stretcher

  • Moderators - CS admin
  • 640 posts
540
  • LocationPantheon of the Hole Stretchers

Posted 26 March 2013 - 08:36 PM

Try it this way. I removed the redudant user inputs.
 

 

#include <stdio.h>main(){      char c;      int result,a,b;                  while (1)      {            printf("Give one of these: + - /.n");                        scanf("%c",&c);            printf("Give an integer.n");            scanf("%d",&a);            printf("Give another integer.n");            scanf("%d",&b );                      if (c=='+')               result=a+b;            else if(c=='-')                 result=a-b;            else                result=a/b;                                   printf("%d %c %d = %d",a,c,b,result);      }return 0; }

 

 

 

switch() case statement?

Do you mean the "case" command which has similar function to the "if" command?

Exactly. When I had to do a similar program, I did it that way.



#10 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 08:52 PM

I tried it the way you recommended, but it bypassed the "scanf("%c",&c);" command again. I also tried it with case and still the same.


...you can achieve immortality, simply by doing one great thing...


#11 Ellanios

Ellanios

    Hole Stretcher

  • Moderators - CS admin
  • 640 posts
540
  • LocationPantheon of the Hole Stretchers

Posted 26 March 2013 - 09:04 PM

Damn this fucking C is always pain in the ass. I used to be good in C only to impress my hot professor cause I wanted to pwn her.

 

But it's been a while since then (5-6 years) so now I can't remember shit.

 

Let me see if I can find the code of my calculator program.



#12 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 09:10 PM

After about half an hour googling, i found this command: fflush(stdin);

I added it above the "printf("Give one of these: + - /.n");" command inside the "while" command.

Finally the bypassing has stopped.

 

Now i'm gonna create three different functions, so to replace the "result=a+b;", "result=a-b;" and "result=a/b;" commands inside main().


...you can achieve immortality, simply by doing one great thing...


#13 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 26 March 2013 - 09:22 PM

"I used to be good in C only to impress my hot professor cause I wanted to pwn her."

 

lol hahaha  :lol:

You lucky boy! My professor is a man, with plait, heavy voice and a T-shirt with an air force stamp on it. lol


...you can achieve immortality, simply by doing one great thing...


#14 Ellanios

Ellanios

    Hole Stretcher

  • Moderators - CS admin
  • 640 posts
540
  • LocationPantheon of the Hole Stretchers

Posted 26 March 2013 - 09:35 PM

I couldn't find the code of my calculator program but it was something like that:

 

#include  <stdio.h>main(){int operation=0,result=0,a=0,b=0; //It's a good practice to initialize the variablesprintf("Select 1 for Addition, 2 for Subtraction, 3 for Division.n");scanf("%d",&operation);switch(operation){case 1:{printf("Give the first number.");scanf("%d",&a);printf("Give the second number.");scanf("%d",&b);result=a+b;printf("The sum is %d.n", result);break;}case 2:{printf("Give the first number.");scanf("%d",&a);printf("Give the second number.");scanf("%d",&b);result=a-b;printf("the remainder is %d.n", result);break;}case 3:{printf("Give the first number.");scanf("%d",&a);printf("Give the second number.");scanf("%d",&b);result=a/b;printf("The quotient is %d.n", result);break;}default:{printf("You have input an invalid number. Try again.n");} }//End of switchreturn 0;}

I just wrote this code after a 6 year hiatus so, forgive any mistakes.

 

Your thread brought me some good memories in my mind. :cheers:



#15 P_W_S Marcello

P_W_S Marcello

    Setti Housetroll

  • Members
  • 662 posts
314
  • LocationLol i dunno, never left my room

Posted 27 March 2013 - 06:49 AM

"I used to be good in C only to impress my hot professor cause I wanted to pwn her."

 

lol hahaha  :lol:

You lucky boy! My professor is a man, with plait, heavy voice and a T-shirt with an air force stamp on it. lol

I will be goin to University in Octobre, and there is one Prof, pretty hot (not a man lol) and one Prof , age around 465 years. Lets hope i dont get Methusalem



#16 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 27 March 2013 - 11:28 AM

I thank you both for your time and your help. The program works great. ;)

 

"Your thread brought me some good memories in my mind.  :cheers:"

 

hehe  :cheers: 


...you can achieve immortality, simply by doing one great thing...


#17 Heady

Heady

    Member

  • Moderators - Admin
  • 2,173 posts
915

Posted 30 March 2013 - 09:21 AM

That's C, not C++, Silent :)



#18 P_W_S Marcello

P_W_S Marcello

    Setti Housetroll

  • Members
  • 662 posts
314
  • LocationLol i dunno, never left my room

Posted 30 March 2013 - 10:04 AM

That's C, not C++, Silent :)

Well, C++ is C just with OOP



#19 Heady

Heady

    Member

  • Moderators - Admin
  • 2,173 posts
915

Posted 30 March 2013 - 10:38 AM

Sure you can mix C++ with C, but here is no C++ part as far as I checked it. You normally don't use sdtdio, printf & scanf in C++ :) BTW, do you study C++ at your university, Silent? We study Java here..



#20 SNiPY

SNiPY

    //Aggressive Gaming™

  • Moderators - Admin
  • 3,534 posts
585
  • LocationGreece

Posted 30 March 2013 - 11:06 AM

I will be goin to University in Octobre, and there is one Prof, pretty hot (not a man lol) and one Prof , age around 465 years. Lets hope i dont get Methusalem

 

lol  :lol:

 

Well, according to my teacher's words, the program is the same for both C and C++. The only different is the compiler. We use this program: Link

 

And we started with C++ the first 6 months. Now we are studying C. Then we will study Java too.  B)


...you can achieve immortality, simply by doing one great thing...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users