Thread: C++ conversion help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    35

    C++ conversion help

    I've finished my assignment in C. But I learned I'm supposed to submit a C++ file. Now I'm trying to convert my program to C++. All I could do is change #include<stdio.h> and #include<stdlib.h> with #include <iostream> and #include <cstdlib>. I learned that C++ codes are able to read C codes too, so I didn't change the code part of my program. But when I compile, I get many errors. While I was working on C file, I didn't get any error and the program worked perfectly. What's wrong in my codes as they're in C++? Any help will be greatly appreciated. Here it is:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    
      struct Student
         {
         char name[20], lettergrade;
         int id, testgrades[10];
         int average;
         int total;
         };
    
         struct Student Stud[81];
    
         void getinfo(int size, struct Student Stud[size]);
         double calculateGrade(int siz, struct Student Stud[siz]);
         void display(int si, struct Student Stud[si]);
    
         int k,s,t,r,g;
         int a[81]={0};
    
      int main(int argc, char *argv[])
         {
         printf("Enter the number of students:");
         scanf("%i", &s);
         printf("\n");
    
         printf("Enter the number of tests:");
         scanf("%i", &t);
         printf("\n");
         getinfo(81, Stud);
    
         system("PAUSE");
         return 0;
         }
    
      void getinfo(int siz, struct Student Stud[siz])
         {
          for(k=1; k<=s; k++){
           printf("Enter %i. student name:", k);
           scanf("%20s", Stud[k].name);
           printf("\n");
           printf("Enter %i. student id:", k);
           scanf("%i", &Stud[k].id);
           printf("\n");
            for(g=1; g<=t; g++){
               printf("Enter %i. test:", g);
               scanf("%i", &Stud[k].testgrades[g]);
               printf("\n");
                               };
           calculateGrade(81, Stud);
           display(81, Stud);
                             };
    
         }
    
      double calculateGrade(int siz, struct Student Stud[siz])
         {
          for(r=1; r<=t; r++){
          Stud[k].total=Stud[k].total+Stud[k].testgrades[r] ; };
          a[k]=(Stud[k].total)/t ;
    
           if( a[k] <= 100 && a[k] >= 85)
           Stud[k].lettergrade='A';
           if(a[k]<85 && a[k]>=75)
           Stud[k].lettergrade='B';
           if(a[k]<75 && a[k]>=65)
           Stud[k].lettergrade='C';
           if(a[k]<65 && a[k]>=50)
           Stud[k].lettergrade='D';
           if(a[k]<50 && a[k]>=0)
           Stud[k].lettergrade='F';
         }
    
         void display(int si, struct Student Stud[si])
         {
           printf("Average is: %i    ", a[k]);
           printf("Letter grade is: %c\n", Stud[k].lettergrade);
           printf("\n");
         }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by farukyaz
    I've finished my assignment in C. But I learned I'm supposed to submit a C++ file.
    Looking at your code, it appears to be a simple assignment of the sort that is used to help students learn a programming language. How on earth could you have made such a mistake? Don't you even know what you are learning?

    Quote Originally Posted by farukyaz
    All I could do is change #include<stdio.h> and #include<stdlib.h> with #include <iostream> and #include <cstdlib>.
    Then you should change to use C++-style I/O.

    Quote Originally Posted by farukyaz
    I learned that C++ codes are able to read C codes too
    Rather, some valid C programs are also valid C++ programs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Here.. change those parameters to the actual types, instead of the index values
    Quote Originally Posted by g++
    foo@bar:~/code$ make
    g++ a.cpp -o a -std=c++0x -Wall -Wextra -g
    a.cpp:15:49: error: use of parameter ‘size’ outside function body
    a.cpp:15:53: error: array bound is not an integer constant before ‘]’ token
    a.cpp:16:57: error: use of parameter ‘siz’ outside function body
    a.cpp:16:60: error: array bound is not an integer constant before ‘]’ token
    a.cpp:17:47: error: use of parameter ‘si’ outside function body
    a.cpp:17:49: error: array bound is not an integer constant before ‘]’ token
    a.cpp:22:7: warning: unused parameter ‘argc’ [-Wunused-parameter]
    a.cpp:22:7: warning: unused parameter ‘argv’ [-Wunused-parameter]
    a.cpp:37:45: error: use of parameter ‘siz’ outside function body
    a.cpp:37:48: error: array bound is not an integer constant before ‘]’ token
    a.cpp:57:54: error: use of parameter ‘siz’ outside function body
    a.cpp:57:57: error: array bound is not an integer constant before ‘]’ token
    a.cpp: In function ‘double calculateGrade(...)’:
    a.cpp:73:6: warning: no return statement in function returning non-void [-Wreturn-type]
    a.cpp: At global scope:
    a.cpp:75:47: error: use of parameter ‘si’ outside function body
    a.cpp:75:49: error: array bound is not an integer constant before ‘]’ token


  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by laserlight View Post
    Looking at your code, it appears to be a simple assignment of the sort that is used to help students learn a programming language. How on earth could you have made such a mistake? Don't you even know what you are learning?
    No, you got it wrong. It's a data structures assignment and the course is made up with both C and C++ codes, because the aim is to teach data structures, rather than a programming language. So it's quite possible for me not to have got in what language I should have done it.


    Quote Originally Posted by laserlight View Post
    Rather, some valid C programs are also valid C++ programs.
    I can't see any of the codes that won't be valid in C++ in this program and this is where exactly I need help.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by manasij7479 View Post
    Here.. change those parameters to the actual types, instead of the index values
    I didn't understand it right, for instance in the following code, what should I make with "int size"? Thanks in advance.

    Code:
    void getinfo(int size, struct Student Stud[size]);

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by farukyaz View Post
    I didn't understand it right, for instance in the following code, what should I make with "int size"? Thanks in advance.

    Code:
    void getinfo(int size, struct Student Stud[size]);
    If the value you'll be passing is an integer, then yes.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by manasij7479 View Post
    If the value you'll be passing is an integer, then yes.
    It works!!! Yes! Thanks for taking serious and helping

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by farukyaz View Post
    It works!!! Yes! Thanks for taking serious and helping
    Works.. as in compiles.. or really works?

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by manasij7479 View Post
    Works.. as in compiles.. or really works?
    Executed many times with different values, and it works great.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    While some C programs are also valid C++ programs, keep in mind that the two languages are very different. The approach to solving problems in C++ is very different from that of C.
    If you are going to learn C++, then you better do it properly instead of just compiling the C code with a C++ compiler. Otherwise you can't really say you know C++ programming.
    It's fine if you don't want to do it, but keep it in mind.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    This thread was reported by farukyaz: "I would like my thread to be deleted in case of the probability that my codes, which is actually part of my assignment, might be stolen or plagiarised."

    Too bad. If you don't want it in the public domain, don't make it public

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Fordy View Post
    This thread was reported by farukyaz: "I would like my thread to be deleted in case of the probability that my codes, which is actually part of my assignment, might be stolen or plagiarised."

    Too bad. If you don't want it in the public domain, don't make it public
    Well, the 1 hour rule is already starting to pay off !

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    35
    Quote Originally Posted by Fordy View Post
    This thread was reported by farukyaz: "I would like my thread to be deleted in case of the probability that my codes, which is actually part of my assignment, might be stolen or plagiarised."

    Too bad. If you don't want it in the public domain, don't make it public
    What's the point of you making this issue more public? And what's the point of you putting me in more difficulty? Please.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The answer is no, as you've experienced. No matter of begging or complaining will change that.
    If someone steals your code, then you are not at fault; (s)he is.
    Your problem and code will help contribute to people having the same problem so they can fix it too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you've already admitted that your code is wrong. knowing this, why would anyone steal it? this is a public forum. everything here is searchable on Google. in fact, I have found threads that I submitted here minutes later in google results. you should be less concerned about your classmates stealing your code, and more concerned with getting it right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bmp to pdf conversion
    By leojose in forum Tech Board
    Replies: 8
    Last Post: 05-29-2005, 09:56 AM
  2. Conversion!!!
    By GT70sgt in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2003, 07:51 PM
  3. Conversion?
    By mart_man in forum C Programming
    Replies: 7
    Last Post: 12-08-2002, 07:23 PM
  4. conversion
    By sonict in forum C++ Programming
    Replies: 10
    Last Post: 11-22-2002, 08:01 PM
  5. Conversion
    By CyberMax8 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 08:46 PM