質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

1546閲覧

C++ 暗号化 謎のエラー

JJJJJJJJJamm

総合スコア10

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2020/04/21 16:34

編集2020/04/21 16:38

前提・実現したいこと

大学の課題で作ったプログラムで、提出する際にエラーメッセージが出るのですが原因が分かりません。どこをどのように直すべきか教えてください。

問題

There is a trick to encrypt the data and use a word as its key. Here's how it works: First, choose a word as the key, such as TRAILBLAZERS. If the word contains repeated letters, only the first one is kept, and the rest are discarded. Now, the modified word is listed below the alphabet, as shown below: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z T R A I L B Z E S Then arrange the letters in the alphabet that do not appear in the modified word after the modified word to get the coding table: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z T R A I L B Z E S C D F G H J K M N O P Q U V W X Y Therefore, based on the key of TRAILBLAZERS, if our original message is ATTACK, it will be encrypted as TPPTAD. Input requirements: input key and original information, output encrypted information Example 1: Input TRAILBLAZERS ATTACK and output TPPTAD. Input : Enter a line, including the input key and the original information (both capital letters), separated by spaces. Output : Output a line, representing the encrypted information

エラーメッセージ

1. The data is opened too small, resulting in access to a memory area that should not be accessed 2. A division by zero error occurs 3. A large array is defined in the function, causing the program stack area to be exhausted 4. The pointer is used incorrectly, resulting in access to a memory area that should not be accessed 5. It is also possible that the program throws an unreceived exception

該当のソースコード

C++

1#include<iostream> 2using namespace std; 3int main() 4{ 5 char list1[26] = {0}, list2[26] = {0};//暗号化する文字列, 新たなアルファベット表 6 int size = 0; char gomi;//入力した文字の文字数 7 bool check = false;// 前に同じ文字が出たかどうか 8 for(int i = 0; ; ++i){ 9 gomi = getchar(); 10 for(int j = 0; j <= i; ++j){ 11 if(gomi == list2[j]) check = true; 12 } 13 if(gomi == ' ') break;//空欄で終了 14 else if(check == true) {i -= 1; check = false; continue;}//前に同じ文字があれば飛ばす 15 else list2[i] = gomi; 16 size += 1; 17 } 18 for(int i = 0; ; ++i){//暗号化する文字列の格納 19 gomi = getchar(); 20 if(gomi == '\n') break;//改行で終了 21 else list1[i] = gomi; 22 } 23 int num = 65;//ASCⅡコード 24 for(int i = size; i < 26; ++i){ 25 for(int j = 0; j < size; j++){ 26 if(num == list2[j]){num += 1; j = -1;}//前に同じコードがあれば確認を最初からやり直し 27 } 28 list2[i] = num; 29 num += 1; 30 } 31 for(int i = 0; i < 26; ++i){ 32 if(list1[i] == 0) break;//暗号化する文字列の出力が終わったら終了 33 cout << list2[list1[i] - 65]; 34 } 35 cout << endl; 36}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gentaro

2020/04/21 17:59

https://teratail.com/help/avoid-asking 「何かを作りたいのでコードを書いてほしい、学校の課題を解いてほしい等の質問は、具体的にプログラミングで困っている質問ではないと考え、推奨していません。」 学校の課題であれば、それまでの講義や教材の中で解決策は示されているはずです。 そうでない場合は講義や教材の質の問題が悪い可能性もあるため、教員に質問・相談しましょう。
JJJJJJJJJamm

2020/04/22 02:23

返信ありがとうございます、著作権のことは初めて知りました。ご指摘ありがとうございます、次からは気をつけます。Tsinghua university, C++Programme Design and Training, Hong Zhao です。助教にも質問しましたが、自信の中国語が拙いこともありあまり理解出来なかったため、こちらでも質問させていただきました。
majiponi

2020/04/22 10:25 編集

How about your English? He will explain what he thinks to you in English if you will, as the whole of the question is written in English, not in 汉语. Maybe asking for "a written answer", such as an e-mail, helps you understand his Chinese, since we can read and share what he thinks. (您能阅懂得汉语吧?)
JJJJJJJJJamm

2020/04/22 12:00 編集

Sorry for being late to reply, I've seen your reply already but I was busy for my classes that I haven't tried your advices yet.....Let me try that in minutes. The original question was written in Chinese, but I translated to English. (我看的懂中文,如果对您来说更方便的话我们就用中文来沟通吧。)
guest

回答2

0

エラーメッセージは大学か教員が用意したようなものですか?
一般的にエラーメッセージはそのプログラムについて起こったエラーについて報告します。
ところが2番のメッセージは0で割り算してると言ってるのに、そもそも割り算が使われてるようには見えません。
単なるありがちな原因を並べただけにも思えます。どういう場合にどのようなメッセージが出されるのか確認してみてください。

そのうえでぱっと見のアルゴリズム上の問題としては

1.暗号化するメッセージの長さが26字以下と決めつけてる
2.keyとなる文字列にアルファベットがすべて含まれていた場合、list2の27番目の要素へのアクセスが発生する

このあたりでしょうか。

投稿2020/04/21 17:52

yudedako67

総合スコア2047

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

JJJJJJJJJamm

2020/04/22 12:32

エラーメッセージとして書いたものはRuntime Errorというエラーが表示された際の主な原因として教員の方から教えていただいたものです。説明不足で申し訳ありません。yudedako67さんのご指摘とても助かりました、どれもうっかりしていて気が付きませんでした。ご回答ありがとうございました。
guest

0

ベストアンサー

I'll give you some tips, which don't answer your questions directly.

  1. Check what getchar() returns; It returns negative values when an error, including EOF, happens. Otherwise this encrypter crashes.

  2. Discard non-capital letters. Same as above.

  3. Write "j < i;" instead of "j <= i" to avoid accessing list2[26].

  4. You don't have to preload the message. You can getchar() after creating the substitution table. Needless to care the length of the message.

  5. Avoid using magic numbers like 65. You can use 'A' to represent that.

投稿2020/04/22 02:19

majiponi

総合スコア1720

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

JJJJJJJJJamm

2020/04/22 12:11

我刚才按照您的回复改变了自己的代码,结果通过了! 好像是因为getchar()哪里有了问题。谢谢您的回复 !
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問