Need Help Running a program in C++ for Ubuntu (Linux)

In summary, the author is having trouble trying to convert a C program into C++. He is following an example, but is having trouble getting the program to work. He wants to run the program on a Linux-based system, not on a Microsoft-based system. If possible, someone can help him.
  • #1
Cam Tay
2
0

Homework Statement


m working on a Engineering Project and I'm having serious trouble trying to run this program in C++. I'm following an example of the program from Program done in C but I'm trying to convert it into C++. I'm going to link the example I'm following and what I'm doing trying to run it in C++. I want this to be able to run in Ubuntu (Linux) based system not for MSDOS (Microsoft) based system. If at all possible, could someone help me try to have this program example run in Ubuntu (Linux Based System) instead of MSDOS (Microsoft Based System)? Please Help.
-------------------------------------------------------------------------------------------------------------------------------------------

Homework Equations


This is the example I'm trying to follow from a C program to change it into C++

void main()
{
char c[10]={0},p[10],d[10]={0};
int i,l,k;
clrscr();
printf("Enter msg:");
gets(p);
printf("\n");
printf("Enter keysize:");
scanf("%d",&k);
puts(p);
for(i=0;i<10;i++)
{
if(p>=65 && p<=96)
{
c=((p-65+k)%26)+65;
}
else if(p>=97 && p<=122)
{
c=((p-97+k)%26)+97;
}
}
printf("\n");
puts(c);
for(i=0;i<10;i++)
{
if(c>=65 && c<=96)
{
if((c-65-k)<0)
{
d=c-k+26;
}
else
{
d=((c-65-k)%26)+65;
}
}
else if(c>=97 && c<=122)
{
if((c-97-k)<0)
{
d=c-k+26;
}
else
{
d=((c-97-k)%26)+97;
}
}
}
printf("\n");
puts(d);
getch();
}
[/B]

The Attempt at a Solution


This is my work example:
//hillciphering.cpp
//Deciphering a Message
//using Hill Ciphering
----------------------------------
#include <iostream>
#include <string>
using namespace std;

void main()
{

char c[10]={0},p[10],d[10]={0};
int i,l,k;

cout << "Enter msg:";
cin >> p;
cout << "\n";
cout << "Enter Matrix Keysize:";
display("%d","&k");
puts(p);
for(i=0;i<10;i++)
{
if(p>=65 && p<=96)
{
c=((p-65+k)%26)+65;
}
else if(p>=97 && p<=122)
{
c=((p-97+k)%26)+97;
}
}
cout << "\n";
puts(c);
for(i=0;i<10;i++)
{
if(c>=65 && c<=96)
{
if((c-65-k)<0)
{
d=c-k+26;
}
else
{
d=((c-65-k)%26)+65;
}
}
else if(c>=97 && c<=122)
{
if((c-97-k)<0)
{
d=c-k+26;
}
else
{
d=((c-97-k)%26)+97;
}
}
}
cout<< "\n";
puts(d);
getch();
}
[/B]
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
1. Use [code][/code] tags to format your code, at the moment it is completely unreadable, as all references to [i] were treated as italic tags.

2. What have you tried, and what have happened? "I have a serious trouble" doesn't tell us anything about what kind of problems you are experiencing.
 
  • #3
Well I'm trying to run it in Ubuntu but it's not running as I would it to in C program. I'm trying to change the few things in C to run it in C++ but it's not working too well for me. Where do I use code/code tags? I don't understand that. Please help
 
  • #4
[code]int main()
{
return 0;
}[/code]

is displayed as

Code:
int main()
{
    return 0;
}

instead of unreadable

int main()
{
return 0;
}

You have still failed to explain what you are doing and what kind of problems you see. There are zillions of things that can go wrong, and not knowing what actions you take and what messages you see means we are not able to help.
 
  • #5


It looks like you are on the right track with your attempt at converting the C program into C++. However, there are a few issues that need to be addressed in order for the program to run successfully on a Linux based system.

Firstly, the main function in C++ should always have a return type of "int" and not "void". So your main function should be declared as "int main()".

Secondly, the "clrscr()" function is a non-standard function and may not work on all systems. It is recommended to use the "system("clear")" function instead, which will clear the screen on a Linux based system.

Thirdly, the "gets()" function is also non-standard and may cause errors. It is recommended to use the "cin.getline()" function instead, which allows you to specify the maximum number of characters to read and avoids buffer overflow issues.

Finally, the "getch()" function is also non-standard and may not work on all systems. It is recommended to use the "cin.get()" function instead, which will wait for user input before exiting the program.

With these changes, your program should be able to successfully run on a Linux based system. If you encounter any further issues, please provide more specific details and error messages so that I can assist you further. Good luck with your project!
 

FAQ: Need Help Running a program in C++ for Ubuntu (Linux)

How do I install C++ on Ubuntu (Linux)?

To install C++ on Ubuntu, you can use the command line by typing sudo apt-get install build-essential. This will install the necessary tools and libraries for compiling and running C++ programs.

How do I compile a C++ program on Ubuntu (Linux)?

To compile a C++ program on Ubuntu, you can use the command g++ followed by the name of your source file. For example, g++ main.cpp. This will create an executable file named a.out in the same directory.

How do I run a C++ program on Ubuntu (Linux)?

To run a C++ program on Ubuntu, you can use the command ./a.out if your executable file is named a.out. Otherwise, you can use the command ./filename to run your program.

How do I debug a C++ program on Ubuntu (Linux)?

To debug a C++ program on Ubuntu, you can use the GNU Debugger (gdb) tool. You can run your program using gdb ./a.out and use the various debugging commands such as break, step, and print to find and fix any errors in your code.

How do I link external libraries in a C++ program on Ubuntu (Linux)?

To link external libraries in a C++ program on Ubuntu, you can use the -l flag followed by the name of the library. For example, to link the math library, you can use the command g++ main.cpp -lm. This will link the math library and allow you to use its functions in your program.

Similar threads

Replies
3
Views
1K
Replies
12
Views
2K
Replies
3
Views
906
Replies
37
Views
4K
Replies
10
Views
2K
Replies
1
Views
9K
Replies
5
Views
2K
Back
Top